Cancel transition, if no input, prevent crash upon no transition

Hi folks, I’m making a mockup for a simple game.  I use transition.to to move two characters towards eachother, and when they collide, pause physics and cancel the ‘losing’ characters transition.  Eventually, I’d like to place spritesheets, but for now I’m just using newRects for placeholders.

On collision, this is what is giving me an issue;

function onCollision( event ) if(event.object1.myName == "dude1" and event.object2.myName == "dude2") then if dude1.x \> display.contentWidth \* 0.5 or dude2.x \> display.contentWidth \* 0.5 then print( "you win!" ) physics.pause() transition.cancel(moveDude2) elseif dude1.x \< display.contentWidth \* 0.5 or dude2.x \< display.contentWidth \* 0.5 then print( "you lose!") physics.pause() transition.cancel(moveDude1) end end

If there is no input from the user, when ‘dude2’ collides with ‘dude1’, moveDude1 has not been called and produces an error.  Input is via a widget button, and ‘dude2’ moves after the screen has been active for two seconds.

Any help would be appreciated, at the moment I’m not quite sure how to go about this, though.

What is ‘no input from user’ - when it is used and how works?

Remember fundamental rule - if you want to do anything physics related or with colliding objects, always do it with delay. Even 10 miliseconds is enough. If you suddenly change physics or physical objects colliding then engine or corona will crush, throw error or any unexpected behwviour will happen

Hi, thanks for the reply.

‘No input from user’ means that, the user did not press a button that triggers the function that contains the moveDude1 tween reference.  Physics seems to behave properly, though that may be different on a device, i’ll remember what you said, though.

The crash comes from the player’s character not moving due to no input(button press), as the transition hasn’t been called at that point.  I’m trying to find a way that I can still cancel the transition upon collision, but also not crash if the transition hasn’t been called. 

Hi @nicefrog,

It’s also essential (in almost every case) that you filter just one of the collision phases, either “began” or “ended”. Collisions trigger both, so you need to handle just one of them or you’ll get multiple events firing off. Typically, collisions are handled on the began phase, but in some cases the ended phase is necessary (like when detecting if an object has left the bounds of a sensor object).

Hope this helps,

Brent

Hey Brent,

As usual, you’re a good help.  I’ll do that for the collision phase and I will work it into the button somehow.

What is ‘no input from user’ - when it is used and how works?

Remember fundamental rule - if you want to do anything physics related or with colliding objects, always do it with delay. Even 10 miliseconds is enough. If you suddenly change physics or physical objects colliding then engine or corona will crush, throw error or any unexpected behwviour will happen

Hi, thanks for the reply.

‘No input from user’ means that, the user did not press a button that triggers the function that contains the moveDude1 tween reference.  Physics seems to behave properly, though that may be different on a device, i’ll remember what you said, though.

The crash comes from the player’s character not moving due to no input(button press), as the transition hasn’t been called at that point.  I’m trying to find a way that I can still cancel the transition upon collision, but also not crash if the transition hasn’t been called. 

Hi @nicefrog,

It’s also essential (in almost every case) that you filter just one of the collision phases, either “began” or “ended”. Collisions trigger both, so you need to handle just one of them or you’ll get multiple events firing off. Typically, collisions are handled on the began phase, but in some cases the ended phase is necessary (like when detecting if an object has left the bounds of a sensor object).

Hope this helps,

Brent

Hey Brent,

As usual, you’re a good help.  I’ll do that for the collision phase and I will work it into the button somehow.