raycast + animation (animation not working properly)

Hello I’ve been having problems with the raycast and animation working together properly. What I am trying to do is get the monster to do a path and do its proper animation in that direction and continue only checking the player direction. The raycast works perfect, and all the logical checks for direction and moving sprite is working great.

The only problem is the animation gets stuck and doesn’t play properly. I been playing with it for days and can’t seem to solve this, I know the animation will get stuck in the gameLoop, so I tried to pull it out and call a function but it seems to also call the function very fast like the gameLoop and getting stuck also. Checking that with the print statement. I am using the MTE and all its functions.

Thanks for your time.

local function monsterAggro(event) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("aggro") &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;mte.cancelSpriteMove(game.sMob\_1,false) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (player.locX == game.sMob\_1.locX and player.locY \> game.sMob\_1.locY) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;game.sMob\_1:setSequence("chickenDown") &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif (player.locX == game.sMob\_1.locX and player.locY \< game.sMob\_1.locY) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;game.sMob\_1:setSequence("chickenUp") &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif (player.locX \< game.sMob\_1.locX and player.locY == game.sMob\_1.locY) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;game.sMob\_1:setSequence("chickenLeft") &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif (player.locX \> game.sMob\_1.locX and player.locY == game.sMob\_1.locY)&nbsp; then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;game.sMob\_1:setSequence("chickenRight") &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;game.sMob\_1:play() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;mte.moveSpriteTo({sprite = game.sMob\_1, locX = player.locX, locY = player.locY, time = 1500, transition = linear}) game.mobState = false end local function gameLoop( event ) if game.sMob\_1 ~= nil then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for a = 1, 360, 20 do &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local startX, startY = game.sMob\_1.x, game.sMob\_1.y &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local endX = game.sMob\_1.x + (game.castDistance \* math.cos(math.rad(a))) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local endY = game.sMob\_1.y + (game.castDistance \* math.sin(math.rad(a))) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local hits = mte.physics.rayCast(startX, startY, endX, endY, "any" ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if hits and game.maxRange == false then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if ( hits[1].object == player and player.level == game.sMob\_1.level ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if game.sMob\_1Hit == false then&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if game.mobState ~= true then -- tried to check certain states but can't get it to work properly --Here is where it calls the function with stuck animation &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;--mte.moveSpriteTo({sprite = game.sMob\_1, locX = player.locX, locY = player.locY, time = 1500, transition = linear, onComplete = monsterAggro()}) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;game.timer\_3 = timer.performWithDelay( 1000, function() monsterAggro(); end, 2 ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end end

Anyone please, I am still having problems with this! Any advice/help/theory would be greatly appreciated just to help steer me in the right direction.

*/poke* Gremlin, always appreciate your advice :slight_smile:

Hey :stuck_out_tongue:

Check the currently playing sequence before setting/playing it, is one option.

I.e.

if sprite.sequence ~= 'up' then sprite:setSequence( 'up' ) sprite:play() end 

That will stop it repeatedly trying to play/reset the up animation if it’s already playing it for instance.

For anyone reading who doesn’t know, yourSpriteName.sequence will return the name of the currently playing (or set) animation sequence for your sprite.

Hope this helps

Works perfectly now, thank you very much Gremlin! I’m glad I */poked* you :slight_smile: It did not occur to me I could logic check for the sprite.sequence…all good now!

Anyone please, I am still having problems with this! Any advice/help/theory would be greatly appreciated just to help steer me in the right direction.

*/poke* Gremlin, always appreciate your advice :slight_smile:

Hey :stuck_out_tongue:

Check the currently playing sequence before setting/playing it, is one option.

I.e.

if sprite.sequence ~= 'up' then sprite:setSequence( 'up' ) sprite:play() end 

That will stop it repeatedly trying to play/reset the up animation if it’s already playing it for instance.

For anyone reading who doesn’t know, yourSpriteName.sequence will return the name of the currently playing (or set) animation sequence for your sprite.

Hope this helps

Works perfectly now, thank you very much Gremlin! I’m glad I */poked* you :slight_smile: It did not occur to me I could logic check for the sprite.sequence…all good now!