Jump to content

Moving to a Pointer


Recommended Posts

I made a small game for moving an image to a location where the use clicks.
The game is set in space where the user is controlling a rocket
The catch to this game is the place the user clicks, the rocket as well as the asteroid moves.
So, the user has to avoid the asteroid and keep moving.
There are two scenes in this game.
Scene 1-
Here is the main game
In preload, we load 2 images

  this.load.image('Ship', 'ship.png');
    this.load.image('asteroid', 'asteroid.png');

In create, we add these images to the scene

 source1 = this.physics.add.image(100, 300, 'Ship');
        source2 = this.physics.add.image(250, 250, 'asteroid');

In update function, we take the input from the cursor and give the position values to a variable. Then we move the images to that position and also check if they collide or not. If they do collide then we switch to scene 2.

this.input.on('pointerdown', function (pointer) {

        target.x = pointer.x;
        target.y = pointer.y;
        target1.x = pointer.x-10;
        target1.y = pointer.y-20;

        this.physics.moveToObject(source1, target, 200);
        this.physics.moveToObject(source2, target1, 50);

    }, this);
this.physics.world.collide(source1, source2, function () {
      source1.setVelocity(0); source2.destroy(); console.log('hit?');

Scene 2-
Scene 2 is just a scene with an image of "You Died".

  preload ()
        this.load.image('died', 'died.png');}
    create ()


Scene 1.jpg

Scene 2.jpg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...