Some problems in the code

Hi, can anyone help me with this code?

There are different errors int the code, but I’m started to learn Lua just 2 days ago…

The code is this:
 

http://pastebin.com/23FwvqFx#

These errors are:

When I click (touch) on the screen

“…\game.lua:157:attempt to index global ‘jetSprite’ (a nil value) stack traceback …”

end when I restart the application
 

“…\game.lua:130:attempt to compare nil with number stack traceback …”

Sorry for the English but I’m Italian :stuck_out_tongue:

Thanks to all!

The line numbers on pastebin don’t quite line up with your original code, but…

  1. You have a function called touchScreen(event). You are not passing in “jetSprite”, so jetSprite is nil. You need to either pass it in or pre-declare it.

  2. You have a variable (not sure which one, but consult the line number) that is being compared with a number. But the variable doesn’t exist within that function, so it becomes nil, which obviously doesn’t work with math. So you need to pass it in, pre-declare, or otherwise find a way to expose it.

Sorry but I didn’t know how to copy correctly the code (if you know how, tell me :slight_smile: )

I understand the errors, but I didn’t understand who are the elements that create these errors

For your first answer, the code with the error is that:

function activateJets(self,event) self:applyForce(0,-1.5,self.x,self.y) end function touchScreen(event) if event.phase == "began" then jetSprite.enterFrame = activateJets() Runtime:addEventListener("enterFrame",jetSprite) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame",jetSprite) end end

You told me to to pass “jetSprite” in or pre-declare it. I not understand how uhm…How I can pass this variable?

For the second answer, I didn’t understand why there is a “nil” ad as value… I can not find the variable with this value

The basic problem is that a variable cannot “leave” a function unless you tell it to. It exists only within the function.

[lua]function flappybird()

  local cooldude = 34

end

print(cooldude) – nil[/lua]

In that example, cooldude is local, so it exists only within the function. If you want it to exist outside, you only have two options:

[lua]

local cooldude

function flappybird()

  cooldude = 34

end

print(cooldude) – 34

[/lua]

This is the “pre-declare” method. You declare the variable first (even if it’s empty) and then update it from the function. Since the variable was made outside of the function it can be seen outside, as well.

[lua]

function flappybird()

 local cooldude = 34

 return cooldude

end

print(flappybird()) – 34

local tempVar = flappyBird()

print(tempVar) – 34

[/lua]

“return” is the other method. When you type return, the function ends immediately and gives you back the value you choose.

In your code, you create jetSprite within a function, but it can’t escape the function. It’s not visible anywhere else. So to everything else, it’s “nil”.

Since you’re looking for it in a touchScreen command (where passing in a variable might be difficult, I think pre-declare is the easiest option for you. You can even declare the variable at the beginning of your file; lots of professional code sometimes starts off with big pre-declares.

Yes, I understand what you mean, but in my case, in the function “touchScreen” there isn’t any local variable…uhm…I’ve declared above the variable “jetSprite”…I should use a “return”?

Based on the pastebin code, you’re creating the local variable “jetSprite” in scene:enterScene(). This is a function, so anywhere outside of that scene, jetSprite is not accessible. This includes in the function you make later, “touchScreen()”

If you want the touchScreen function to see jetSprite, you should:

  1. Predeclare somewhere near the beginning of the file.

[lua]local jetSprite[/lua]

  1. Since you pre-declared, do not use local when you create jetSprite in enterScene().

[lua] jetSprite = something – if you declare local here, then you’re making a local, seperate jetSprite, not one assigned to the predeclare. So just omit “local” [/lua]

Yes, right! I’ve just fixed it, but now there is a problem with this code:
 

function activateJets(self,event)     self:applyForce(0,-1.5,self.x,self.y) end  

The error is the same:
 

"....\game.lua:157:attempt to index global 'self' (a nil value) stack traceback ...."  

I’m not sure I understand how “self” works… this is the cause of the error of which I have spoken

The problem there is that the function is looking for “self”, which usually means you called a function using . instead of :

[lua]tabbyCat:roll()
– is the same as
tabbyCat:roll(tabbyCat)[/lua]

If you use . then ‘self’ is not carried over, and you get ‘nil’ instead.

Yes but I call this function with “self” as parameter because while I touch the screen the jet must go up in the sky and when I release it must go down. I didn’t understand why it not run and give me the error… What’s wrong?

…ok…I understand where is the error…when I call “activateJets” I’ve done it in this mode: “activateJets()” while when I call this function in this mode “activateJets” it’s ok… Why this? What’s the difference?

Ok, I’ve solved all problems :smiley: thanks for all! I hope that I’ll not have other problems :slight_smile:

Hi guys, I’ve other problems, but now with the physic objects… I need to make a customize shape for my character, so I’ve done it. But I have a problem with the physic because when I touch the screen, the body don’t go on vertically but it starts to rotate on itself…

At first time, Corona had created a automatically body (big rectangle). But I’ve a problem with the collisions with other objects because they touch invisible parts of the figure (in game), and so I lose though it’s not true. So I’ve created two different rectangles and I’ve add them to my graphics object. But when I touch the screen, the object go on incorrectly and starts to rotate on itself. 

This is the original shape:

https://www.dropbox.com/s/185o7okoivfm7hf/imgpersonal.png

And this is my custom shape: 

https://www.dropbox.com/s/xub29ol8c0auyas/imgoriginal.png

Can anyone help me please?

Ok, solved  :stuck_out_tongue:

The line numbers on pastebin don’t quite line up with your original code, but…

  1. You have a function called touchScreen(event). You are not passing in “jetSprite”, so jetSprite is nil. You need to either pass it in or pre-declare it.

  2. You have a variable (not sure which one, but consult the line number) that is being compared with a number. But the variable doesn’t exist within that function, so it becomes nil, which obviously doesn’t work with math. So you need to pass it in, pre-declare, or otherwise find a way to expose it.

Sorry but I didn’t know how to copy correctly the code (if you know how, tell me :slight_smile: )

I understand the errors, but I didn’t understand who are the elements that create these errors

For your first answer, the code with the error is that:

function activateJets(self,event) self:applyForce(0,-1.5,self.x,self.y) end function touchScreen(event) if event.phase == "began" then jetSprite.enterFrame = activateJets() Runtime:addEventListener("enterFrame",jetSprite) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame",jetSprite) end end

You told me to to pass “jetSprite” in or pre-declare it. I not understand how uhm…How I can pass this variable?

For the second answer, I didn’t understand why there is a “nil” ad as value… I can not find the variable with this value

The basic problem is that a variable cannot “leave” a function unless you tell it to. It exists only within the function.

[lua]function flappybird()

  local cooldude = 34

end

print(cooldude) – nil[/lua]

In that example, cooldude is local, so it exists only within the function. If you want it to exist outside, you only have two options:

[lua]

local cooldude

function flappybird()

  cooldude = 34

end

print(cooldude) – 34

[/lua]

This is the “pre-declare” method. You declare the variable first (even if it’s empty) and then update it from the function. Since the variable was made outside of the function it can be seen outside, as well.

[lua]

function flappybird()

 local cooldude = 34

 return cooldude

end

print(flappybird()) – 34

local tempVar = flappyBird()

print(tempVar) – 34

[/lua]

“return” is the other method. When you type return, the function ends immediately and gives you back the value you choose.

In your code, you create jetSprite within a function, but it can’t escape the function. It’s not visible anywhere else. So to everything else, it’s “nil”.

Since you’re looking for it in a touchScreen command (where passing in a variable might be difficult, I think pre-declare is the easiest option for you. You can even declare the variable at the beginning of your file; lots of professional code sometimes starts off with big pre-declares.

Yes, I understand what you mean, but in my case, in the function “touchScreen” there isn’t any local variable…uhm…I’ve declared above the variable “jetSprite”…I should use a “return”?

Based on the pastebin code, you’re creating the local variable “jetSprite” in scene:enterScene(). This is a function, so anywhere outside of that scene, jetSprite is not accessible. This includes in the function you make later, “touchScreen()”

If you want the touchScreen function to see jetSprite, you should:

  1. Predeclare somewhere near the beginning of the file.

[lua]local jetSprite[/lua]

  1. Since you pre-declared, do not use local when you create jetSprite in enterScene().

[lua] jetSprite = something – if you declare local here, then you’re making a local, seperate jetSprite, not one assigned to the predeclare. So just omit “local” [/lua]

Yes, right! I’ve just fixed it, but now there is a problem with this code:
 

function activateJets(self,event)     self:applyForce(0,-1.5,self.x,self.y) end  

The error is the same:
 

"....\game.lua:157:attempt to index global 'self' (a nil value) stack traceback ...."  

I’m not sure I understand how “self” works… this is the cause of the error of which I have spoken

The problem there is that the function is looking for “self”, which usually means you called a function using . instead of :

[lua]tabbyCat:roll()
– is the same as
tabbyCat:roll(tabbyCat)[/lua]

If you use . then ‘self’ is not carried over, and you get ‘nil’ instead.

Yes but I call this function with “self” as parameter because while I touch the screen the jet must go up in the sky and when I release it must go down. I didn’t understand why it not run and give me the error… What’s wrong?

…ok…I understand where is the error…when I call “activateJets” I’ve done it in this mode: “activateJets()” while when I call this function in this mode “activateJets” it’s ok… Why this? What’s the difference?