Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Move a sprite continuously toward choosen 2d point

Author
Message
Timshark
17
Years of Service
User Offline
Joined: 30th Jun 2007
Location: Oslo, Norway
Posted: 11th Nov 2017 19:25
Hi

I want to move a sprite toward a choosen 2d point. But I want it to continue and not stop at the point.
I've found the example of moving toward a mouse clicked point but I just want it to continue past the clicked point in a set speed.

How is this done

Tim
Conjured Entertainment
AGK Developer
19
Years of Service
User Offline
Joined: 12th Sep 2005
Location: Nirvana
Posted: 12th Nov 2017 00:07 Edited at: 12th Nov 2017 00:23
SetSpritePosition(spriteID, GetSpriteX(spriteID)+1, GetSpriteY(spriteID))

That is one way, where that is moving the sprite on its X axis 1 pixel each cycle.

Of course that could be any number you want, and you could do the same for the Y axis if you wanted.

If you Know the fixed value for Y then you could just use that like...

SetSpritePosition(spriteID, GetSpriteX(spriteID)+1, 200)

But the first example just reads whatever it currently is already set to in case it has already been adjusted by something else.

You can do a -1 instead of +1 to move the other direction, and you want to have some kind of limit, so putting that in an IF statement that checks the position would be best.

That way you will not keep moving it until it gets out of bounds and causes an error.

In your case you would have a couple of variables that are representing the fixed point you want to move to for the X and Y.

Then, the IF statement(s) would compare the sprite's current X & Y positions to the desired location variables and then increment or decrement the position accordingly.



That limits it between 0 and 700 so you will not go out of bounds, so if the desired_x is 100 or 400 and you current position is 200, then it will move you in that direction and beyond it, but still stay in the limits.

Hope this makes sense and helps, and I am sure someone else will now tell you a better way to do it.

Coding things my way since 1981 -- Currently using AppGameKit V2 Tier 1
easter bunny
12
Years of Service
User Offline
Joined: 20th Nov 2012
Playing: Dota 2
Posted: 12th Nov 2017 00:46 Edited at: 12th Nov 2017 00:47
I'd do it like this:


My Games - Latest WIP - My Website: Immortal.Digital - FB - Twitter
130,000 installs with AppGameKit and counting
smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 12th Nov 2017 15:39
life's one big game
spec= 4ghz, 16gb ram, AMD R9 2700 gpu
Timshark
17
Years of Service
User Offline
Joined: 30th Jun 2007
Location: Oslo, Norway
Posted: 12th Nov 2017 17:30
Easter Bunny

Thanks, that´s exactly what I need.

It was the len and direction calculations I was looking for

Great Help

Tim

Login to post a reply

Server time is: 2024-11-24 12:48:25
Your offset time is: 2024-11-24 12:48:25