Transition on enemies

I’m trying to animate a couple of enemies between two points. The problem is that on the second turn one enemy get all the transitions and the other enemies just sand there.

The one that get all the animations (the first one) start hopping around every time a transition finishes.

Why?!

[code]
local function animeraFiende_Normal(inFiende, inTid, vandPunkt_L, vandPunkt_R)

function fiende_L()
print(inFiende.alive)
if inFiende.alive == “ja” then
print(“Vänster”)
print(inFiende.namn)
transition.to( inFiende, { time=inTid, x=vandPunkt_L, transition = easing.inOutQuad, onComplete=fiende_R } )
transition.to( inFiende, { xScale=-1 } )
end
end
function fiende_R()
if inFiende.alive == “ja” then
print(“Vänster”)
print(inFiende.namn)
transition.to( inFiende, { time=inTid, x=vandPunkt_R, transition = easing.inOutQuad, onComplete=fiende_L } )
transition.to( inFiende, { xScale=1 } )
end
end
fiende_L()
end
[/code] [import]uid: 28895 topic_id: 7365 reply_id: 307365[/import]

This got to be a bug? Why does it work for one circulation fiende_L()+ fiende_R() but ehen it returns to fiende_L() the last object get all the transitions?
[import]uid: 28895 topic_id: 7365 reply_id: 25976[/import]

the bug is in your code, not corona.

what is the rest of your code where you call the animation function? [import]uid: 6645 topic_id: 7365 reply_id: 26039[/import]

Ok, that feels better. Then there is a way to fix it. :slight_smile:

But what have i done wrong?

I’m calling the function for the tween like this

  
animeraFiende\_Normal(enemey1, 3000, 0, 300)  
  
animeraFiende\_Normal(enemey2, 5000, 100, 300)  
  
animeraFiende\_Normal(enemey3, 7000, 100, 480)  

enemey1, enemey2 and enemey3 is display objects. [import]uid: 28895 topic_id: 7365 reply_id: 26071[/import]

even though you create your Fiende_L and Fiende_R functions inside a local function they are still global functions, so they get mixed up with each other

without getting into the whole OOP/metatable function solution, i’d do this for now:

[lua]local fiende_L – forward references
local fiende_R – so functions can see each other

function fiende_L(inFiende) – still local due to forward reference

if inFiende.alive == “ja” then
print(“L”, inFiende.namn)
transition.to( inFiende, { time=inFiende.params.inTid, x=inFiende.params.vandPunkt_L, transition = easing.inOutQuad, onComplete=fiende_R } )
transition.to( inFiende, { xScale=-1 } )
end
end

function fiende_R(inFiende) – still local due to forward reference
if inFiende.alive == “ja” then
print(“R”, inFiende.namn)
transition.to( inFiende, { time=inFiende.params.inTid, x=inFiende.params.vandPunkt_R, transition = easing.inOutQuad, onComplete=fiende_L } )
transition.to( inFiende, { xScale=1 } )
end
end

local function animeraFiende_Normal(inFiende, inTid, vandPunkt_L, vandPunkt_R)

inFiende.params = {inTid = inTid, vandPunkt_L = vandPunkt_L, vandPunkt_R = vandPunkt_R}
fiende_L(inFiende)
end[/lua] [import]uid: 6645 topic_id: 7365 reply_id: 26095[/import]

jmp909, thank you so much! Taught i had tried everything. Probable me usually do Java, JS or AS messed up my head.

Tank you again. :slight_smile: [import]uid: 28895 topic_id: 7365 reply_id: 26223[/import]

If I want the transition to run an enterFrame function while it transitions so i can check some values meanwhile and if some events occur i can cancel the transition or pause it. How do i do that? [import]uid: 28895 topic_id: 7365 reply_id: 26255[/import]

[lua]local checkSomething
local enemy=display.newImage(“enemy.png”)

local start()

enemy.trans = transition.to(enemy, {time=10000, rotation=360})

end

local function doSomething()
if(enemy.trans~=nil) then
transition.cancel(enemy.trans)
end
end

local function onEnterFrame(e)
if(checkSomething==“whatever”) then
doSomething()
end

Runtime:addEventListener(“enterFrame”, onEnterFrame)[/lua] [import]uid: 6645 topic_id: 7365 reply_id: 26259[/import]

Problem is i use the director class.

It starts the “gameEngine” Witch import “level_x” witch imports "fiender " and so fort trough

This one inside “level_x” who is imported from “gameEngine” witch is loaded by director:

[code]local fiender = require(“fiender”)

local fiende_1 = fiender.fiendeTyp_1(display.screenOriginX + 150, display.screenOriginY + display.contentHeight - 90, 3000, 0, 480)
[/code]

How do i check up on my enemies in the enterframe witch i created in other functions?

Still thinking lua is a bit strange on this.

For example. On this level i have 3 enemies(=fiende). If i wanna check if one of this enemies is on the same Y as my bomb and then pause this enemies animation while the bomb passes them how do i do that?
in fiender.lua:

[code]
function fiendeTyp_1(inX, inY, inTid, vandPunkt_L, vandPunkt_R)
nummerFiender = nummerFiender + 1;

local fiende_1 = display.newImage( “fiende_1.png”, true )
fiende_1.x = inX
fiende_1.y = inY
physics.addBody( fiende_1, “kinematic”, { density=2.0, friction=0.5, bounce=0.8 } )
fiende_1.class = “orginalFiendeTyp”

animeraFiende_Normal(fiende_1, inTid, vandPunkt_L, vandPunkt_R, “orginalFiendeTyp_”…nummerFiender)

return fiende_1
end
[/code] [import]uid: 28895 topic_id: 7365 reply_id: 26423[/import]

you might want to look into [lua]dispatchEvent[/lua]

for example you add the listener in your main class
[lua]fiender1:addEventListener(“myCustomEvent”, doSomething)[/lua]

and then in your fiender.lua use dispatchEvent to dispatch that custom event.
http://developer.anscamobile.com/reference/index/objectdispatchevent

that should work although i haven’t tried it. it’s how you dispatch between classes in flash though, and corona follows a similar model to some extent [import]uid: 6645 topic_id: 7365 reply_id: 26436[/import]

I’m sorry. I don’t understand how that will help me check if the bomb is in close proximity of any of my enemies in my enterFrame function you helped me do?

Should it be very hard on the game if i made i global table for enemies in “fiender.lua” that contained all the enemies and the enterframe looped trough them? [import]uid: 28895 topic_id: 7365 reply_id: 26951[/import]

i was talking about dispatching an event from the fiender module to the main module for instance, i wasn’t answering your other question. [import]uid: 6645 topic_id: 7365 reply_id: 26997[/import]

My second question then? I have done the table solution now and it works fine. But the question is on slower devices is it going to be a problem? [import]uid: 28895 topic_id: 7365 reply_id: 27031[/import]