Pause/Resume Button Problem - Sprites

Hi, I have a pause/resume button that was working perfectly until I put some sprites to the game.

Well, in the pause function, I put a instance:pause() but it just doesn’t work!

It gives this error:

Runtime error ...umes/ServerT4/Conteudos/Games/Projeto/jogo.lua:251: attempt to index global 'instance' (a nil value) stack traceback: [C]: ? ...umes/ServerT4/Conteudos/Games/Projeto/jogo.lua:251: in function <...umes><br> ?: in function <?:215><br>

So, the game freezes, but the sprite keep going on. [import]uid: 75946 topic_id: 13693 reply_id: 313693[/import] </…umes>

Hey there,

See here; http://developer.anscamobile.com/reference/index/spriteinstancepause

Peach :slight_smile: [import]uid: 52491 topic_id: 13693 reply_id: 50403[/import]

Yeah, it is already like that!

Let me show you the part of the codes that’ll probably show my error:
[lua] local Proj = sprite.newSpriteSheet(“images/sprite.png”, 50, 50)
local spriteProj = sprite.newSpriteSet(Proj, 1, 3)
sprite.add(spriteProj, “Proj”, 1, 3, 350, 0)
local inProj = sprite.newSprite(spriteProj)
inProj.myName = “Proj”
inProj.y = -25
randomProj = math.random(33.75, _W - 33.75)
inProj.x = randomProj
inProj:prepare(“Proj”)
inProj:play()
physics.addBody(inProj, “dynamic”, { density = 5.0, friction =0.0, bounce = 0.0})
inProj.isFixedRotation = true
local function resumegame()
physics.start()
pause = false
game = true
inProj:play()
pausebtn.isVisible = true
resumebtn.isVisible = false
end
local function pausegame()
physics:pause()
inProj:pause()
pause = true
game = false
pausebtn.isVisible = false
resumebtn.isVisible = true
end[/lua] [import]uid: 75946 topic_id: 13693 reply_id: 50455[/import]

Hey, it says that “instance” is a nil value, yet I can’t see it in your code above.

Which line of the above do you think is triggering the error, and where is this “instance” that is a nil value?

Peach :slight_smile: [import]uid: 52491 topic_id: 13693 reply_id: 50556[/import]

251 would be inProj:play()
if i remove/comment this line
the error goes to the 257 line(which is the inProj:pause())

And it is inProj, not instance the error.

[lua]Runtime error
…umes/ServerT4/Conteudos/Games/Projeto/jogo.lua:257: attempt to index global ‘inProj’ (a nil value)
stack traceback:
[C]: ?
…umes/ServerT4/Conteudos/Games/Projeto/jogo.lua:257: in function <…umes>
?: in function <?:215>[/lua] [import]uid: 75946 topic_id: 13693 reply_id: 50959[/import] </…umes>

Here is your code, slightly altered so I could test it. (You just need this in your main.lua and pinch the greenman sprite sheet out of your Corona SDK folder, Sprites, Jungle Scene.)

[lua]require “sprite”
require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )
local pausebtn = display.newRect( 0, 30, 20, 10 )
local resumebtn = display.newRect( 100, 30, 20, 10 )

local Proj = sprite.newSpriteSheet( “greenman.png”, 128, 128 )
local spriteProj = sprite.newSpriteSet(Proj, 1, 15)
sprite.add(spriteProj, “Proj”, 1, 15, 200, 0)
local inProj = sprite.newSprite(spriteProj)
inProj.myName = “Proj”
inProj.y = -25
randomProj = math.random(33.75, _W - 33.75)
inProj.x = 160
inProj.y = 240
inProj:prepare(“Proj”)
physics.addBody(inProj, “dynamic”, { density = 5.0, friction =0.0, bounce = 0.0})
inProj.isFixedRotation = true
local function resumegame()
physics.start()
pause = false
game = true
inProj:play()
pausebtn.isVisible = true
resumebtn.isVisible = false
end
resumebtn:addEventListener(“tap”, resumegame)

local function pausegame()
physics:pause()
inProj:pause()
pause = true
game = false
pausebtn.isVisible = false
resumebtn.isVisible = true
end
pausebtn:addEventListener(“tap”, pausegame)[/lua]

Try that. It gives me no errors; do you get any? Let me know.

It is almost entirely the same code, so I’m very curious to see if it solves your issue. (Obviously you’d swap out that sprite for yours, but you get the idea.)

Peach :slight_smile: [import]uid: 52491 topic_id: 13693 reply_id: 51084[/import]

Yeah, it didn’t work at all.

Same error, I just changed the sprite to mine and the number of sheets(I think it’s that what it is supposed to be about)
[lua]Runtime error
…umes/ServerT4/Conteudos/Games/Projeto/jogo.lua:260: attempt to index global ‘inProj’ (a nil value)
stack traceback:
[C]: ?
…umes/ServerT4/Conteudos/Games/Projeto/jogo.lua:260: in function <…umes>
?: in function <?:215>[/lua]

This error is driving me crazy!


EDIT:

I’m using your code and it work PERFECTLY, I’ll try hard to adapt it to my game.

THANK YOU A LOT.

EDIT 2:

I think I got what’s the trigger for the error, and that would be the inProj:play() line. It activates after inProj:prepare(“proj”). If I delete it, the pause/resume button works just fine!

But the point is, I want the sprite to act since the applications runs, is there any way I could do that without getting a enormous error? [import]uid: 75946 topic_id: 13693 reply_id: 51363[/import] </…umes>

It’s hard to say as the code I gave you worked fine prior to you switching to your own sprite, as you said.

What version of Corona are you using and are you on a Mac or a PC? [import]uid: 52491 topic_id: 13693 reply_id: 51540[/import]

I’m using Corona on a Mac and the version is 2011.591.

Yeah, it’s really awkward that it doesn’t work, I just changed the sprite and put a inProj:play().

I have no idea what’s going on. [import]uid: 75946 topic_id: 13693 reply_id: 51604[/import]

Is there a reason you need to have that line at the start? I thought it automatically played without it in my example?

Are you trying to play it when it’s already playing? [import]uid: 52491 topic_id: 13693 reply_id: 51718[/import]

No, it doesn’t play when the game starts, so I call that line to start the sprite movement. [import]uid: 75946 topic_id: 13693 reply_id: 51766[/import]

Hmmmm.

Are you able to share your sprite or is it something you want kept private? You could turn the images to black in Photoshop and post it if so?

I’m just thinking I might be able to take a better look at it for you if I had your actual spritesheet, as I can’t see why my code works, plays, pauses, etc. and yours doesn’t - and although it’s not exactly our job, I’d still like to have a go at finding a solution for you.

Update the thread and let me know :slight_smile:

Peach [import]uid: 52491 topic_id: 13693 reply_id: 51860[/import]

I already changed the sprite sometimes and it keeps the error. I don’t think the problem is the sprite, because of that.

The thing about your example code is that is doesn’t starts with the opening, we need to press the button to make it work, that’s my problem, in rest it all works.

PS: I haven’t change ANY coding, but now it doesn’t give ANY error, but the sprite is still not pausing.

PS2:Do you have any faster way to contact you? Like messenger, an email or anything? I think I would fix it sooner if so. [import]uid: 75946 topic_id: 13693 reply_id: 52134[/import]

Alright, I get it - use this code;

[lua]require “sprite”
require ( “physics” )
physics.start()
physics.setGravity( 0, 0 )

_W = 320
_H = 480

local pausebtn = display.newRect( 0, 30, 20, 10 )
local resumebtn = display.newRect( 100, 30, 20, 10 )

local Proj = sprite.newSpriteSheet( “greenman.png”, 128, 128 )
local spriteProj = sprite.newSpriteSet(Proj, 1, 15)
sprite.add(spriteProj, “Proj”, 1, 15, 200, 0)
local inProj = sprite.newSprite(spriteProj)
inProj.myName = “Proj”
inProj.y = -25
randomProj = math.random(33.75, _W - 33.75)
inProj.x = 160
inProj.y = 240
inProj:prepare(“Proj”)
physics.addBody(inProj, “dynamic”, { density = 5.0, friction =0.0, bounce = 0.0})
inProj.isFixedRotation = true

local function resumegame()
physics.start()
pause = false
game = true
inProj:play()
pausebtn.isVisible = true
resumebtn.isVisible = false
end
resumebtn:addEventListener(“tap”, resumegame)

local function pausegame()
physics:pause()
inProj:pause()
pause = true
game = false
pausebtn.isVisible = false
resumebtn.isVisible = true
end
pausebtn:addEventListener(“tap”, pausegame)

resumegame()[/lua]

It’s written for the greenman sprite again, but that should do it. It plays when the app loads, it pauses when you click the button, plays when you tell it to again, etc.

I’m just calling the resume function immediately. Take a look :slight_smile:

Let me know - but I THINK this is exactly what you want. [import]uid: 52491 topic_id: 13693 reply_id: 52263[/import]

Sorry for taking SO long to answer. But I have decided to recode my game and guess what? It all works now!

Thank you SO SO SO SO SO SO SO much for your assistance.

I’m really happy with Corona and it’s community! [import]uid: 75946 topic_id: 13693 reply_id: 52920[/import]

Glad to hear it - and grats on the recode working out well for you :slight_smile: Sometimes starting fresh can help, especially when you’ve learned a lot throughout the process and wish to implement it better :wink:

Peach [import]uid: 52491 topic_id: 13693 reply_id: 53021[/import]