Change to next scene when player overlaps with door?

A new question, which I can’t figure out.

I want my player to finish the level, when he overlaps with the door.

I tried this (using that director thing):

if player.x == door.x then director:changeScene( "loadlevel2" )

But it does nothing.

It is also strange that sometime x-y coordinates are counted from the border of the image and sometimes from the center. Or am I stupid and confuse something? [import]uid: 6587 topic_id: 5714 reply_id: 305714[/import]

Never mind!

I found it in the other topic, which I didn’t see previously:
http://developer.anscamobile.com/forum/2011/01/29/completing-level

function levelComplete(event) if player.x \> door.x -10 then director:changeScene( "loadlevel2" ) end end Runtime:addEventListener("enterFrame",levelComplete)

It works like a charm.


It’s nice to talk to myself on this forum :stuck_out_tongue: [import]uid: 6587 topic_id: 5714 reply_id: 19568[/import]

http://developer.anscamobile.com/reference/index/objectsetreferencepoint

Use this to shift the x and y =) [import]uid: 9577 topic_id: 5714 reply_id: 19578[/import]

I lied.

Unfortunately it doesn’t work, how I want it.

I don’t understand LUA really…

With the code the level changes to the loading screen, but then instead of loading the next level, it crashes and I get an error about the first level with “attempt to perform arithmetic on field ‘x’ (a nil value)

What does it even mean?
And if the next scene is already loading, why then the crash due to something, which is already not there?

Or is it still there?

I don’t understand memory handling here…

I can’t find a solution which would perform the “remove event listener”… I assume that would be necessary…

Can somebody tell me, how I supposed to change scenes if I don’t want to use buttons, scores swipes and similra for that? [import]uid: 6587 topic_id: 5714 reply_id: 19619[/import]

function levelComplete(event)  
if player.x \> door.x -10 then  
director:changeScene( "loadlevel2" )  
Runtime:removeEventListener("enterFrame",levelComplete)  
end  
end  
Runtime:addEventListener("enterFrame",levelComplete)  
  

or use this function since Director Class calls this when changing screens(at least mine does, don’t know which version)

[code]
unloadMe = function()

Runtime:removeEventListener(“enterFrame”,levelComplete
end
[/code] [import]uid: 10657 topic_id: 5714 reply_id: 19626[/import]

haha I know!

Is your player a physics object? [import]uid: 10657 topic_id: 5714 reply_id: 19684[/import]

My player has a body for being able to collide with walls. It’s a kind of a maze game. I have no gravity in the game and no bounce or friction or such. I just gave my player (and the walls) the “hasBody” attribute.

I also made a Rect with isSensor True and placed above the door, where the player escapes the level.

I also have collectibles in the game, so I think I will need this sensor stuff often for letting my player pick up different things and to count those. [import]uid: 6587 topic_id: 5714 reply_id: 19694[/import]

Thanks!
It worked, but then I experienced other problems with the math.
The unloadme function doesn’t work for me somehow…

Why did nobody tell me, that there are sensors? :stuck_out_tongue:

To be honest, it’s a bit frustrating here…
I find only useless examples of codes in the doc and api list…

Now I made a sensor on my door, but I can’t figure out, how to check, if the player collides/overlaps with the sensor.

No matter how hard I try the search function, there is not a single useful post, which explains how to say that “if objectX collides with sensorY, then do A”

Everything is loaded with complicated and redundant crap…grrrrr

So, if anybody could tell me please, how to write this… and please without pre-post-collision-redballoon-print-myname-began-crap

Thanks in advance. [import]uid: 6587 topic_id: 5714 reply_id: 19647[/import]

Try this!

[code]
local function onCollision( self, event )

if self.myName == “Player” and event.other.myName==“sensor” then
director:changeScene( “loadlevel2” )

– print( self.myName … ": collision began with " … event.other.myName )

end

end

sensor.myName = “sensor”

player.myName = “Player”
player.collision = onCollision
player:addEventListener( “collision”, player )
[/code] [import]uid: 10657 topic_id: 5714 reply_id: 19695[/import]

Thanks!

It works, however I had to add a timer to it.

It seems, it is not the math, which causes the problem. No matter if I use the x cordinates to determine the exit, or the sensor, I always get an error, because it says, I attempt to perform arithmetic on field ‘x’ (a nil value).

It comes, because of my buttons (like this one)

[code]local buttonright = display.newImage( “images/right.png”,440,255 )
localGroup:insert(buttonright)
local function moveTargetRight( )
player.x = player.x + 2
if player.xScale == -1 then
player.xScale = 1
end

end
local function acceleratorright( event )
local this = event.target
local phase = event.phase
if “began” == phase then
– Start Focus
display.getCurrentStage():setFocus( this , event.id )
this.isFocus = true
Runtime:addEventListener( “enterFrame”, moveTargetRight)
elseif this.isFocus then
if “ended” == phase or “cancelled” == phase then
– End Focus
display.getCurrentStage():setFocus( this , nil )
this.isFocus = false
Runtime:removeEventListener( “enterFrame”, moveTargetRight)
end
end
end
buttonright:addEventListener( “touch”, acceleratorright )[/code]

If I move the player with x = x+2, on the exit it can’t calculate x+2 anymore and crashes.

At least I know now, how sensors work :stuck_out_tongue:

With the timer it works, but then I have another problem. But I make another topic for that. [import]uid: 6587 topic_id: 5714 reply_id: 19704[/import]

But I think that error is because you don’t unload the eventlisteners

But I could be wrong! [import]uid: 10657 topic_id: 5714 reply_id: 19706[/import]

Yes, I think so too, but I made another topic for that. [import]uid: 6587 topic_id: 5714 reply_id: 19707[/import]

It seems to be fine, but I am a bit confused, how I would use it.
But I’ll try to find it out.

Thanks! [import]uid: 6587 topic_id: 5714 reply_id: 19790[/import]

Sensors aren’t documented much yet because they’re pretty new. For your purpose you could use the hitTest function I wrote and avoid messing with sensors:

http://developer.anscamobile.com/code/flashs-hittestobject-emulated-using-contentbounds [import]uid: 12108 topic_id: 5714 reply_id: 19762[/import]