level transitions

right now I am trying to get the code so when the character hits the endpoint of the level a level complete screen comes up and it transitions to the next level. I’m doing this by calling the characters x and y values and when he hits the x and y of the end point it will change. What i am having trouble with is the level switching. I will post what code i have on here any help would be great thanks. [blockcode] function endPoint(event)
rich.x = x
rich.y = y
if(rich == x and rich == y) then
levelComplete:prepare(“levelcomplete.png”)
levelComplete:play()
end
end
Runtime:addEventListener( “enterFrame”, endPoint )
levelComplete:addEventListener( “touch”, levelcomplete.png)

here is the code from the level

gameplay.endpoint(50,100)
changeScene:prepare(“oneone”)
changeScene:play() [import]uid: 94237 topic_id: 22527 reply_id: 322527[/import]

This doesn’t look right;

[lua]levelComplete:addEventListener( “touch”, levelcomplete.png)[/lua]

I doubt you have a function called levelcomplete.png :wink: [import]uid: 52491 topic_id: 22527 reply_id: 89795[/import]

right, i have edited that out for now. I want to get it working first off. It is saying that prepare is a nil value. after looking at it it must have to do with the changescene, i don’t think there is a changescene in the file. This seems to be where i’m stumped. Would it have endPoint in the front? [import]uid: 94237 topic_id: 22527 reply_id: 89805[/import]

Where is the code setting up your changeScene sprite? Above or below this? [import]uid: 52491 topic_id: 22527 reply_id: 89840[/import]

there is no sprite, it is supposed to be an event triggered when the character is at the x,y point put in (gameplay.endpoint(50,100)) [import]uid: 94237 topic_id: 22527 reply_id: 89991[/import]

I am using a boolean trigger now that looks like

levelComplete = false
if character x,y = endpoint x,y then
levelComplete = true

function levelComplete
if levelComplete = true then
change scene

the problem is since i am calling the x,y how do I put that in without it looking like gamplay.endPoint(50,100) like earlier. that wont work in this [import]uid: 94237 topic_id: 22527 reply_id: 90066[/import]

Is that your exact code above, or just an example?

I can’t tell WHY you can’t do it like the last level; need more explanation/code explaining. [import]uid: 52491 topic_id: 22527 reply_id: 90088[/import]

that was just an example, I noticed something that is much easier and slipped by me some how. Instead of using the endPoint just make it equal to the endButton so now its like this [code]
levelComplete = false
if (endButton = hitbox ) then
levelComplete = true
end
function levelComplete
if (levelComplete = true) then
director:changeScene(oneone)
end
end

i need to put a hit box of the character in there, i’m not sure how to call a hitbox. [import]uid: 94237 topic_id: 22527 reply_id: 90100[/import]

its not accepting the endbutton = hitbox anyone know a way around this? [import]uid: 94237 topic_id: 22527 reply_id: 90129[/import]

I’m not totally sure what a hitbox is in this case - and you should be using == if questioning whether or not two things are equal.

What is hitbox? Endbutton and hitbox would need to be the same thing for that to fire. [import]uid: 52491 topic_id: 22527 reply_id: 90143[/import]

I think you are misunderstanding a lot of fundamentals here which is making things difficult:

  1. 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.

  1. 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.

  2. 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]

yes this is what i was trying to do, thanks so much for your help. Just to be clear are these functions supposed to be in the same file? Also it keeps saying assertion failed and I have no clue where the error is from. [import]uid: 94237 topic_id: 22527 reply_id: 90280[/import]

my colleges and I figured it out, we had to tweek it a little to work with what we had but it works great! Thanks so much for your help nick and peach, love the otter. [import]uid: 94237 topic_id: 22527 reply_id: 90401[/import]

Glad to hear you figured it out :slight_smile:

Nick that was an EXCELLENT and detailed post; thoroughly enjoyed reading it :slight_smile: [import]uid: 52491 topic_id: 22527 reply_id: 90405[/import]

No problem, there’s not much else to do on my lunchbreak! :slight_smile:

Fancy swapping jobs, Peach? :wink: [import]uid: 93133 topic_id: 22527 reply_id: 90477[/import]

Haha, I think I’ll pass - where else could I match my work to my nocturnal lifestyle? :wink: [import]uid: 52491 topic_id: 22527 reply_id: 90648[/import]

hospital? [import]uid: 10389 topic_id: 22527 reply_id: 90649[/import]

haha i like how the focus of this thread has changed. I vote you could as batman. [import]uid: 94237 topic_id: 22527 reply_id: 90654[/import]

*Grin* [import]uid: 10389 topic_id: 22527 reply_id: 90657[/import]

I’m far more likely to emulate Harley Quinn, given the choice :wink:

I enjoy how threads switch like this too, it’s great to have some friendly chatter :slight_smile: [import]uid: 52491 topic_id: 22527 reply_id: 90695[/import]