I have a game in which you click an area of the screen and the character walks to it. I want the character to move at 90 degrees only, so horizontally and vertically, with no diagonal. I have used a fair few if statements inside the enterFrame, and the character walks a bit jerkily - I guess because there is too much calculations to be done every frame.
My code is below. If you can think of any better way to optimise it please let me know.
scene.character.onEnterFrame = function(){
//---walk horizontally first
if(doneWalkingX == false){
//--are you in front or behind?
if(targetX > this._x){
distanceX = targetX - this._x
scene.character.gotoAndStop("walk_right")
if(distanceX > walkSpeed){
this._x += walkSpeed
}else{
doneWalkingX = true
}
}else if(targetX < this._x){
scene.character.gotoAndStop("walk_left")
distanceX = this._x - targetX
if(distanceX > walkSpeed){
this._x -= walkSpeed
}else{
doneWalkingX = true
}
} else{
//--bang on
trace("bang on X")
doneWalkingX = true
}
}
//--walk vertically second
if(doneWalkingX == true and doneWalkingY == false){
//--are you in front or behind?
if(targetY > this._y){
distanceY = targetY - this._y
scene.character.gotoAndStop("walk_down")
if(distanceY > walkSpeed){
this._y += walkSpeed //--move
this.swapDepths(this._y) //--be in front or behind of the objects on screen
}else{
doneWalkingY = true //--this means we are finished
}
}else if(targetY < this._y){
distanceY = this._y - targetY
scene.character.gotoAndStop("walk_up")
if(distanceY > walkSpeed){
this._y -= walkSpeed //--move
this.swapDepths(this._y) //--be in front or behind of the objects on screen
}else{
doneWalkingY = true //--this means we are finished
}
} else{
//--bang on
trace("bang on Y")
doneWalkingY = true
}
}
if(doneWalkingX == true and doneWalkingY == true){
//--finished walking
//--now do the thing they've set out to do
_root[doingAction](currentTargetItem) //--because we might do different things after walking, call one of many functions. The action tells us what
doingAction=""
statusText = ""
delete(this.onEnterFrame)
trace("done walking. xpos of character = "+scene.character._x)
}
}
Math.abs() returns the absolute value of the number, that is, no minus sign Thus, the result of the division of the distance by its absolute value can only be 1 (if distance is positive) or -1 (if it’s negative), which adjusts the speed variable according to the relative position of your character to the target position. The same works for the Y axis.
However, even though the code can be optimized, it doesn’t justifies the said jerkness of movement. Most probably it’s an animation problem, but I can only confirm that by seeing the thing working. Maybe the speed of animation doesn’t match the real movement speed, or maybe it needs more smooth animation…
bpalermo, I like your code because you have cut several if statements down to one, but it doesnt solve the problem of telling the character which way to face. I will still need another IF statement to work out if he should face left or right.
Any ideas?
[quote=onion;2328378]bpalermo, I like your code because you have cut several if statements down to one, but it doesnt solve the problem of telling the character which way to face. I will still need another IF statement to work out if he should face left or right.
Any ideas?[/quote]
You mean the setting the image to the right facing, right? It moves but doesn’t change the sprite to the correct direction, is that it? You’re right I left your line regarding face, but I didn’t put it where it should be, my fault. =)
LOL! Unrelated to the actual code, but the current graphics actually remind me of one of my nightmares! You should keep the graphics as is because if you don’t create a game with graphics like these I WILL!
I can imagine a game with these graphics being very weird with strange logic such as ‘use black thing with green fish’
Hell yes, you should see some of the malformed malnourished freakophiles that live on the estate I reside on. I’d love to move it’s just not a good time at the moment. I swear the kids there are all born with extra limbs.
Make a game about that: escaping the freaks of Bolton. I imagine it like 28 days later, but instead of swarms of zombies, swarms of chavs. Your goal could be to get to a safe-house in Burnley.
Yeah, and extra hard enemies could be customised ford Novas and old Fiestas with huge spoilers on and thumping beats coming out of, full of no less than 30 occupants each wearing a baseball cap and some form of tracksuit, their lifeless brains causing them to bop to the music.
The good thing is you’ll hear the customised 1 litre engine popping through the customised 40cm wide exhaust long before they get close!