Pro programmers - is this the way to do it?

Hi there pros!

I am in the first stage of my game and have started out with the control of movement and change of sprite sequences - depending on the direction the character is moving.

A touch on the screen makes the character fly, and a drag makes him move to that direction. And if you drag to the opposite, he turns and flies at the other direction. This involves three animations.

So, we start with the first codeblock where I check to see in what direction the player wants the character to fly.

local function getSpriteSeq(event)  
  
 SPRITE\_STATE = event.sprite.sequence  
  
end   
  
Runtime:addEventListener("touch", moveforward)  
  
local function moveforward( event)  
  
 --\> Save startposition when dragging  
 if(event.phase=="began") then  
 x0 = event.x  
  
 end  
 --\> Check what direction we are going for...  
 if(event.phase=="moved") then  
  
 if not (player==nil) then  
  
 if(x0 \< event.x) then  
  
 --\> Flying forward  
  
 player:applyLinearImpulse(.02, 0, player.x, player.y)   
  
 if(SPRITE\_STATE=="fly\_left") then  
 display.getCurrentStage():setFocus(nil)  
  
 player:prepare("turn\_right")  
 player:play()   
 end  
 else  
 --\> Flying backward  
 player:applyLinearImpulse(-.02, 0, player.x, player.y)  
 if(SPRITE\_STATE=="fly\_right") then  
 display.getCurrentStage():setFocus(nil)  
 player:prepare("turn\_left")  
 player:play()   
 end  
 end  
  
 end  
  
 end  
 display.getCurrentStage():setFocus( nil )  
   
end  
  

So first thing i do is to check what sprite sequence we are dealing with. I added “getSpriteSeq” as a listener to the player sprite.In that function I collect the current animation name. I then added a listener for touch input to check in what direction the user want the animations to go. If going backward, I start to play the “Turn back” sequence. And vice-versa if the player wants to go forward. These animations are short and turns the animation around.

Then I have this listener that checks if the “turn” animation is done. If so - the play the regular fly animation.

local function spriteListener( event )  
  
 if(SPRITE\_STATE=="turn\_left") then  
  
 if(event.sprite.currentFrame==20) then  
 --\> The turn left animation is done, now play the fly left.  
 player:prepare("fly\_left")  
 player:play()   
 end  
 end  
  
 if(SPRITE\_STATE=="turn\_right") then  
 if(event.sprite.currentFrame==20) then  
 --\>The turn right animation is done, now play fly right.  
 player:prepare("fly\_right")  
 player:play()   
 end  
 end   
end  

So my question is basically, is this the correct way to handle the animations sequences - or is there a better way? Is this bad programing or could it work for a sharp release?

Best regards, Joakim [import]uid: 81188 topic_id: 15204 reply_id: 315204[/import]