I think you are misunderstanding a lot of fundamentals here which is making things difficult:
- You have tried to set up a variable ‘gameplay.endpoint’ to store x and y values for where the character must hit.
However, gameplay.endpoint(50,100) will call the endpoint function within gameplay.lua, and send the parameters 50 and 100 to it. This function doesn’t exist in the code you’ve posted, although you do have a function called endPoint (variables and functions are case-sensitive).
Even if you corrected the spelling, the endPoint function is not set up to receive the parameters (50,100), it is just a function that is fired every frame as an ‘enterFrame’ listener.
-
Even if the above did work as you intended, unless you are manually moving the player in 1-pixel increments, it is unlikely the characters position will ever EXACTLY match x=50, y=100.
-
As Peach says, when comparing two variables or objects, you need to use ‘==’, and for that to work, endButton and hitbox would need to be same object. I think what you mean is that you want an invisible box, that will fire your end of level functions when the player hits it?
So, first let’s set up a function to create an invisible box for your character to hit, and add an event listener that fires whenever something hits this box:
[lua]
local target
local createTarget = function (left, top, width, height)
target = display.newRect(left, top, width, height)
target.alpha = 0 – comment this out until you are happy with the placement of the box
target:addEventListener(“collision”,targetHit)
localGroup:insert(target) – change to your main display group
end[/lua]
You can then call this function at the start of each level using:
[lua]createTarget(40, 90, 20, 20)[/lua]
…which will create a 20x20 pixel box, 40 pixels from the left of the screen and 90 from the top (making its centre equal to your 50,100 requirements).
Now we need to handle the collision event, we want to call the end level code when the player hits this box (I assume your character object is called ‘rich’?)
[lua]local targetHit = function (event)
local other = event.other – gets the object that hit the box
if event.phase == “began” and other == rich then
levelComplete()
end
end[/lua]
You will need to put the targetHit function above the createTarget function in your code, and call ‘createTarget’ below the function itself.
From here you just need to set up a function called levelComplete, that creates your level complete screen and switches to the next level. [import]uid: 93133 topic_id: 22527 reply_id: 90160[/import]