Problem With my Code

Hey everyone,

I am currently making a game which is written in lua, I want it to change the background image when the player goes in a certain place. I have written this code but it will not work.

if ((player.x <= display.contentHeight / 3.8) and (player.x >= display.contentHeight / 3.5) and (player.y >= display.contentWidth / 100)) then

print(“entered town 1”)

end

Is there anything else that I could to do fix this problem?

Thank you

Where have you put this code? It will need to be in an enterFrame listener that runs every frame, or a timer that runs every x milliseconds.

Your code checks .x with .height and .y with width.
Try checking .x with .width and .y with .height instead

Hey, thanks for the responce but it is landscape which is why i’ve done that. I’ve used a little cross to figure out which figures I should use for location. Thank you though

Whether it’s portrait or landscape, width relates to the x axis and height the y axis.

For example, on iPad1, reported height is 1024 and width 768 in portrait, and in landscape height is 768 and width 1024.

You didn’t answer where this code is called btw.

 I have written this code but it will not work.

if ((player.x <= display.contentHeight / 3.8) and (player.x >= display.contentHeight / 3.5) and (player.y >= display.contentWidth / 100)) then

print(“entered town 1”)

end

Where have you put this code - a timer, a listener, a button?

Hey Nick… I thought you’d left for “greener pastures”?

Yeah I don’t work with Corona anymore but still dip into the forums, a bit like seeing what an ex is up to on Facebook…

Ive only just started using lua tbh. I know a lot of python so I have knowledge of some things in lua. I’m really bad at the event listener stuff haha. In Python the if statement is all you need lol. What would I need to add to call this? Thanks

The point is unless you put the code in some sort of listener or function that is called regularly, it will only execute once, and your player is unlikely to be in the right place.

[lua]

local gameLoop = function()

// we want code in here to run once a frame

end

Runtime:addEventListener(“enterFrame”, gameLoop) // this is how we make it happen

[/lua]

lol… so what are you doing now mate?

Thank you so much man for writing this code! It’s helped so much and it’s working great now. Thank you!!

Also, in python you can do import time and time.sleep(1). How do you implement time in lua?

you will need to think differently in Corona as you cannot sleep code without it blocking the main thread.

I would suggest you study the tutorials and guides and work through the sample apps.

Hey again, sorry but for some reason whenever I add this infinite loop to my main.lua file, corona doesn’t respond.

while true do

if justEnteredViridisTown2 == 1 then

if enteredViridisTown2 == 1 then

player.x = display.contentWidth / 2

player.y = display.contentHeight / 1.5

justEnteredViridisTown2 = 0

end

end

end

that is because it is an infinite loop

You answered your own question, it’s an infinite loop. It will keep executing until the while condition is not fulfilled, in this case this is never.

Porting my game to Unity, while also contracting as a Unity Developer at a mobile games firm in Brighton.

Maybe I wrote the question wrong. I meant that whenever I add this code to corona, and I then go to start up corona simulator, instead of starting up my app, it is not responding and crashes.

You are blocking the main thread with never-ending code, so your app will appear to not be responding. It never has a chance to update the screen or do anything else, so the os assumes it is not responding.