eduar.londono

How Drag sprite around circle

Recommended Posts

in fact, since you're request was actually to drag the sprite, I've made a version where you need to actually hold the mouse down on it to move it

http://phaser.io/sandbox/hftdIYxX/play

 

the only improvement I'd like to make, but am not currently sure about, is to only move the sprite when the pointer is actually over it.

 

j

Share this post


Link to post
Share on other sites

here's one way to do it using Sin/Cos

http://phaser.io/sandbox/gXbgAMnK/play

 

and another using phaser's point rotation function (with a distance constraint... http://phaser.io/docs/2.4.4/Phaser.Point.html#rotate)

http://phaser.io/sandbox/wDpGcNHm/play

 

I've taken 0 angle as the top of the circle, but that's not the normal way angles are measured so i've shifted the value accordingly

 

 

I'll leave you a challenge... add acceleration/deceleration to the movement.... :)

Share this post


Link to post
Share on other sites

jmp909

 

Thanks. That worked perfectly.  But I guess you knew it would :)  (fyi I used your first suggestion)

 

I know I can alter speeds of the left/right keys by adding:

 

    if(leftKey.isDown) {        angle+=speed+4        moved=true    }

I know this is not what you asked -- so I will do some digging to see what I can come up with as aoptions.

Share this post


Link to post
Share on other sites

I have battled this problem myself recently and came up with this (working!) solution:

 

function angle(cx, cy, ex, ey) {
    var dy = ey - cy;
    var dx = ex - cx;
    return Math.atan2(dy, dx);
}

function update() {
    var r = 44;

    var bulltargetAngle = angle(
        circleCenter.x, circleCenter.y,
            this.game.input.activePointer.x, this.game.input.activePointer.y);


    bullet.y = player.y + Math.sin(bulltargetAngle) * r;
    bullet.x = player.x + Math.cos(bulltargetAngle) * r;
}

In the above code a sprite named "bullet" rotates around the circle pattern following mouse pointer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.