Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Problem With my Code
Started by 64danieldavies Feb 01 2018 01:05 PM

- - - - -
27 replies to this topic
player location

Best Answer nick_sherman , 03 February 2018 - 03:46 AM

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.

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

[TOPIC CONTROLS]
Page 1 of 2 1 2
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

64danieldavies

[GLOBAL: userInfoPane.html]
64danieldavies
  • Observer

  • 15 posts
  • Corona SDK

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


[TOPIC: post.html]
#2

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,868 posts
  • Corona SDK

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.



[TOPIC: post.html]
#3

Arteficio

[GLOBAL: userInfoPane.html]
Arteficio
  • Enthusiast

  • 83 posts
  • Corona SDK

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

[TOPIC: post.html]
#4

64danieldavies

[GLOBAL: userInfoPane.html]
64danieldavies
  • Observer

  • 15 posts
  • Corona SDK

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



[TOPIC: post.html]
#5

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,868 posts
  • Corona SDK

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.

[TOPIC: post.html]
#6

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,868 posts
  • Corona SDK

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

[TOPIC: post.html]
#7

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,120 posts
  • Corona SDK

 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"?



[TOPIC: post.html]
#8

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,868 posts
  • Corona SDK

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

[TOPIC: post.html]
#9

64danieldavies

[GLOBAL: userInfoPane.html]
64danieldavies
  • Observer

  • 15 posts
  • Corona SDK

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

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



[TOPIC: post.html]
#10

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,868 posts
  • Corona SDK

  Best Answer

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.

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


[TOPIC: post.html]
#11

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,120 posts
  • Corona SDK

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

 

lol.... so what are you doing now mate?



[TOPIC: post.html]
#12

64danieldavies

[GLOBAL: userInfoPane.html]
64danieldavies
  • Observer

  • 15 posts
  • Corona SDK

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.

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

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



[TOPIC: post.html]
#13

64danieldavies

[GLOBAL: userInfoPane.html]
64danieldavies
  • Observer

  • 15 posts
  • Corona SDK

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



[TOPIC: post.html]
#14

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,120 posts
  • Corona SDK

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.



[TOPIC: post.html]
#15

64danieldavies

[GLOBAL: userInfoPane.html]
64danieldavies
  • Observer

  • 15 posts
  • Corona SDK

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


[TOPIC: post.html]
#16

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,120 posts
  • Corona SDK

that is because it is an infinite loop...



[TOPIC: post.html]
#17

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,868 posts
  • Corona SDK

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.

[TOPIC: post.html]
#18

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,868 posts
  • Corona SDK

lol.... so what are you doing now mate?


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

[TOPIC: post.html]
#19

64danieldavies

[GLOBAL: userInfoPane.html]
64danieldavies
  • Observer

  • 15 posts
  • Corona SDK

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.

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.



[TOPIC: post.html]
#20

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,868 posts
  • Corona SDK

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.

[TOPIC: post.html]
#21

64danieldavies

[GLOBAL: userInfoPane.html]
64danieldavies
  • Observer

  • 15 posts
  • Corona SDK

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.

Ok, thank you, but how can I change this code to work? Thank you everyone so much for your help so far!



[TOPIC: post.html]
#22

nick_sherman

[GLOBAL: userInfoPane.html]
nick_sherman
  • Corona Geek

  • 1,868 posts
  • Corona SDK

Well this looks like code that you want to continually check for a certain set of conditions and then do something when met, so you can put this in an enterFrame listener too.

[TOPIC: post.html]
#23

SGS

[GLOBAL: userInfoPane.html]
SGS
  • Corona Geek

  • 2,120 posts
  • Corona SDK

In the nicest possible way you are trying to run before you can walk.... you need to work through the tutorials and guides I linked for you so you understand.



[TOPIC: post.html]
#24

64danieldavies

[GLOBAL: userInfoPane.html]
64danieldavies
  • Observer

  • 15 posts
  • Corona SDK

In the nicest possible way you are trying to run before you can walk.... you need to work through the tutorials and guides I linked for you so you understand.

 

Thank you man, don't worry haha, I'm not someone who gets offended very often haha. It's just that my college course is very fast and we are trying to make our own apps at the moment.



[TOPIC: post.html]
#25

64danieldavies

[GLOBAL: userInfoPane.html]
64danieldavies
  • Observer

  • 15 posts
  • Corona SDK

Well this looks like code that you want to continually check for a certain set of conditions and then do something when met, so you can put this in an enterFrame listener too.

It's working now! Thank you all so much!!




[topic_controls]
Page 1 of 2 1 2
 
[/topic_controls]