Help Removing Certain Objects

Hello,

I’m using director and I have a system so that when you click it spawns a crate on the screen. I have a restart button which goes to a loading page. It works for everything but the crates - the crates stay where they are, and each time I restart it adds +1 crate per click each time, so first go each click is one crate, the restarted it’s 2 crates per click.

Any ideas on how to make it work. Here’s my restart button:

[code]local function res(event)
goToScene = “level1”
fxMode = “crossfade”
crate:removeSelf()
director:changeScene(“loadlevel”, crossfade)
end

resBt:addEventListener(“tap”, res)[/code]

and this is the code for spawning crates:

[code]local function spawnCrate(event)
local crate = display.newImage(“images/crate.png”)
crate.x = event.x; crate.y = event.y;
physics.addBody( crate, { density=15.0, friction=0.5, bounce=0.2} )
end

Runtime:addEventListener(“tap”, spawnCrate)[/code]

Thanks, any help is much appreciated. [import]uid: 68047 topic_id: 11656 reply_id: 311656[/import]

Can you simply add the crates to your localGroup? :slight_smile: [import]uid: 52491 topic_id: 11656 reply_id: 42392[/import]

Well, I want it so it spawns several crates, not one, but I’ll try the local group thing and get back to you.

EDIT: I don’t know whether doing your localgroup idea would give the desired result. My current code works, but if I start adding things to localGroup, how will I make them spawn at event.x and event.y?

EDIT: Thanks, I tried it out and should’ve have doubted you. It works perfectly after adding localGroup:insert(crate) into the spawnCrate function. I didn’t fully understand you at first, but thanks so much! [import]uid: 68047 topic_id: 11656 reply_id: 42397[/import]

OK, it works for the first restart, but not for the rest. Any ideas? [import]uid: 68047 topic_id: 11656 reply_id: 42401[/import]

Hey, glad it is at least partially working :wink:

When you say it doesn’t work after the first restart; do you mean a simulator restart or a restart using a button?

Are you getting errors? What exactly is happening?

Peach :slight_smile: [import]uid: 52491 topic_id: 11656 reply_id: 42476[/import]

Basically, it places crates fine, then you click the restart button and they disappear fine. Then you try adding more crates and it works, but there’s and error whilst trying to “insert” into the localGroup.

Then when you restart they don’t disappear, and you start adding n + 1 crates at a time.

Thanks for your help. [import]uid: 68047 topic_id: 11656 reply_id: 42490[/import]

Ok, now it doesn’t add an extra crate per click per restart, so that’s sorted, but the restart button still only works once. I might pastebin it and give the link?

Thanks. [import]uid: 68047 topic_id: 11656 reply_id: 42496[/import]

It might not hurt; I can’t guarantee that I’ll personally be able to look at it, but someone might.

IF I have time I’ll try to take a look.

Peach :slight_smile: [import]uid: 52491 topic_id: 11656 reply_id: 42658[/import]

level1.lua:
http://pastebin.com/DBwRwaKV

and this is the loadlevel.lua which is called when restart button is hit:
http://pastebin.com/ZrW2khep [import]uid: 68047 topic_id: 11656 reply_id: 42694[/import]

Anybody? I know I’m getting close. The error I am getting now relates to “insert” so I assume it has something to do with reinserting it back into localGroup…

Exact error: [code] …istrator\Desktop\CoronaProjects\Game\MAIN\level1.lua:81: attempt to c
all method ‘insert’ (a nil value)
stack traceback:
[C]: in function ‘insert’
…istrator\Desktop\CoronaProjects\Game\MAIN\level1.lua:81: in function
<…istrator topic_id:="" reply_id:=""></…istrator>

Going to BUMP this as I’m sure somebody can figure it out! :slight_smile: [import]uid: 68047 topic_id: 11656 reply_id: 42854[/import]

You’re using director but not with the standard code at the top and bottom of the file.

After module(…, package.seeall) do

[lua]function new()
local localGroup = display.newGroup()
[/lua]

At the very end of your file, do;

[lua]return localGroup
end[/lua]

That should fix it. [import]uid: 52491 topic_id: 11656 reply_id: 42878[/import]

Thanks…
But, I completely scrapped everything other than what you just told me, the spawnCrate function and the restart button. Same error, same problem except now it’s not adding an extra crate per click per restart now, so it’s now 1 crate per click only.

Also, which may be useful to know, when restarting the load level screen doesn’t allow crates to spawned on the first restart (because it’s a seperate file) but afterwards it does.

So, same problem: crates disappear once, but not afterwards. Error is to do with inserting into localGroup. loadlevel.lua also experiences the problem, even though it’s a seperate lua file.

[import]uid: 68047 topic_id: 11656 reply_id: 42889[/import]

NEVERMIND: I’ve solved it. It was because it was a Runtime listener, so I switched it back to a listener for the background image and it works. Now all I need to do it change back to Runtime and clean properly across levels, correct?

Thanks for all your help! [import]uid: 68047 topic_id: 11656 reply_id: 42892[/import]