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.

2D All the way! / how to stop sprite from moving??? :O

Author
Message
chan
12
Years of Service
User Offline
Joined: 14th Nov 2011
Location:
Posted: 14th Nov 2011 17:03
i am currently making a 2D RPG game which whenever you click on a location, the unit will move to that way..(nothing unusual).

i have managed to make it move but i cant make it stop

MOVE by CLICK CODE:


i am not familiar to the other codes which other uses, this code of mine is from draft and is originally written by myself.

-Excellence deals with weak people-
Hodgey
14
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 21st Nov 2011 13:15 Edited at: 21st Nov 2011 13:15
What I would do is use a boolean to determine when your sprites can move. So hypothetically:



Something along those lines is what I'd use.

chan
12
Years of Service
User Offline
Joined: 14th Nov 2011
Location:
Posted: 22nd Nov 2011 09:26
i already tried this one, and i guess this was my first code made. I really thought it would work but it just keeps on going --"

but i will try again if its just my problem, not the code.

-Excellence deals with weak people-
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 24th Nov 2011 06:09
When you click a location, your unit walks in that direction, right? So how about when you click a location, you determine the distance that the unit should move; and when that distance is covered, it stops.

To stop a sprite from moving:



In your code above, you should (at a minimum) change:



without the assignment, the temp variable never changes. Without a mouse-click or escape-key, there is no other exit from the while...loop unless you plan on quiting the entire game...

However, looking at your code, you are (or will be) having other issues. For example, given your existing code above, when the sprite is moving, *nothing* else will be happening. No AI (artificial intelligence) can think, no burning buildings can animate (not entirely true but bare with me here) or no bullets can fly around.

Why? The short answer is that you're not giving them a chance to.

Again, you may ask: Why? Because you are placing the entirety of the sprite's movement distance within a single while...loop. That means that while the sprite is moving, nothing else *can* happen.

Animated sprites/objects are (potentially) different because somewhere within LoopGDK() or dbSync() [I'm not sure which exactly] they are handled by GDK; not neccessarily correctly, but they are handled somewhere by one of those two functions...

So, instead of having the sprite move in "one fail-swoop" so to speak, you should have it move in increments. At what increment, you ask? Well, a frame-rate increment! What do I mean by that...



Notice in this snippet, that the selected_id only advances at an incremented rate; in this case, one unit per frame. Each frame of the game will allow the selected_id to move 1-unit towards the targeted position while allowing the user to interact, the buildings to burn and bullets to fly by...

With all that said, Good-Luck and Happy Coding.

JTK
Gor4k
12
Years of Service
User Offline
Joined: 25th Oct 2011
Location:
Posted: 1st Dec 2011 01:24
just set



DGDK-C++ Developer

Login to post a reply

Server time is: 2024-04-20 06:07:47
Your offset time is: 2024-04-20 06:07:47