Removing disks in my game....

My app is made for iPad. Viewed Portrait, there is a red bar at the bottom of the screen and a blue bar at the top. When a user taps on the screen, a disk appears. I made it so that the disk is either blue or red. There is also gravity, so the disks fall down to the red bar. I want to make it so that when a blue disk touches the red bar, it is removed, but the red disks will do nothing. Since I don’t have a definitive name for the disks, I am not sure what to call to remove(in the last function)?

Here’s my code…

[code]local redBase = display.newImage(“redBase.png”)
redBase.y = 1008
physics.addBody(redBase, “static”, {bounce = 1, friction = 1})
local blueBase = display.newImage(“blueBase.png”)
blueBase.y = 15
physics.addBody(blueBase, “static”, {bounce = 1, friction = 1})

local diskGfx = { “redDisk.png”, “blueDisk.png”}
local allDisks = {} – empty table for storing objects

local function spawnDisk( event )
local phase = event.phase
if “ended” == phase then

randImage = diskGfx[math.random( 1, 2 )]
allDisks[#allDisks + 1] = display.newImage( randImage )
local disk = allDisks[#allDisks]
disk.x = event.x; disk.y = event.y
disk.rotation = math.random( 1, 360 )
disk.xScale = 0.8; disk.yScale = 0.8

physics.addBody( disk, { bounce= 1, density=1, friction=1, radius= 30.0 } )
disk.linearDamping = 0.4
disk.angularDamping = 0.6
end
end
background:addEventListener( “touch”, spawnDisk ) – touch the screen to create disks
local function removeBlueDisk(event)
if(event.phase == “began”) then
disk:removeSelf()
end
end
redBase.collision = removeBlueDisk
redBase:addEventListener(“collision”, removeBlueDisk)
[/code]

Sorry about the bland title, I didn’t know what the problem I am having would be called… [import]uid: 7116 topic_id: 6873 reply_id: 306873[/import]

Just a quick edit (haven’t tried it):
[lua]local redBase = display.newImage(“redBase.png”)
redBase.y = 1008
–Adding this so the disk can determine what color bar it’s hitting
redBase.color = “red”
physics.addBody(redBase, “static”, {bounce = 1, friction = 1})
local blueBase = display.newImage(“blueBase.png”)
–Adding this so the disk can determine what color bar it’s hitting
blueBase.color = “blue”
blueBase.y = 15
physics.addBody(blueBase, “static”, {bounce = 1, friction = 1})

local diskGfx = { “redDisk.png”, “blueDisk.png”}
local allDisks = {} – empty table for storing objects

–Move this up here so we can add it to the blue disks
local function removeBlueDisk(event)
–Only remove the disk if the collision began and it’s a red bar
if(event.phase == “began” and event.other.color == “red”) then
disk:removeSelf()
end
end
local function spawnDisk( event )
local phase = event.phase
if “ended” == phase then

randImage = diskGfx[math.random( 1, 2 )]
allDisks[#allDisks + 1] = display.newImage( randImage )
local disk = allDisks[#allDisks]
disk.x = event.x; disk.y = event.y
disk.rotation = math.random( 1, 360 )
disk.xScale = 0.8; disk.yScale = 0.8
physics.addBody( disk, { bounce= 1, density=1, friction=1, radius= 30.0 } )
disk.linearDamping = 0.4
disk.angularDamping = 0.6
–If it’s a blue disk, add the collision handler
if (randImage == “blueDisk.png”) then
disk.collision = removeBlueDisk
disk:addEventListener(“collision”,removeBlueDisk)
end
end
end
background:addEventListener( “touch”, spawnDisk ) – touch the screen to create disks[/lua]
Hope that helps! [import]uid: 8782 topic_id: 6873 reply_id: 23979[/import]

Thanks, it looks good! I’ll try it out tomorrow morning, late here on the east coast. [import]uid: 7116 topic_id: 6873 reply_id: 23988[/import]

It doesn’t work, nothing happens when the disk hits the bar… in terminal it said something about disk being a global object, so I deleted local, and it kind of worked. When a red disk hit the bar nothing was done (good), but if the blue disk hit the bar, it would remove the last disk created, whether it was red or blue. Here’s my project:

Thanks!

http://rapidshare.com/files/448975110/Project.zip [import]uid: 7116 topic_id: 6873 reply_id: 24050[/import]

yes what’s “disk” in that case? it doesn’t refer to anything. where have you created a global variable “disk”? you haven’t that’s why it’s nil. you only created a “disk” local to spawnDisk. it can’t be seen outside spawnDisk. plus even if it could you wouldn’t want to use one variable to try refer to multiple disks

the fix you require is below:

[lua]local function removeBlueDisk(event)
–Only remove the disk if the collision began and it’s a red bar
if(event.phase == “began” and event.other.color == “red”) then
event.target:removeSelf()

– or
– local someDisk = event.target
– someDisk:removeSelf()
– someDisk=nil
end
end[/lua] [import]uid: 6645 topic_id: 6873 reply_id: 24138[/import]

Works, thanks, especially for explaining :slight_smile: [import]uid: 7116 topic_id: 6873 reply_id: 24147[/import]

I want to make the red disks float up, while the blue disks go with gravity. The blue disks already go w/ gravity, but I can’t figure out how to get the red disks to float up. I tried to accomplish it in the function opposeGravity, but I feel like it’s horribly wrong. And i promise this is my last question :slight_smile: I’ll find some way to repay you guys for your help :slight_smile:

local physics = require("physics")  
physics.start()  
physics.setGravity(0,4)  
local topBG = display.newImage("topBG.png")  
local bottomBG = display.newImage("bottomBG.png")  
bottomBG.y = 740  
local divider = display.newImage("divider.png")  
divider.y = 512  
  
local redBase = display.newImage("redBase.png")  
redBase.y = 1009  
--Adding this so the disk can determine what color bar it's hitting  
redBase.color = "red"  
physics.addBody(redBase, "static", {bounce = 1, friction = 1})  
local blueBase = display.newImage("blueBase.png")  
--Adding this so the disk can determine what color bar it's hitting  
blueBase.color = "blue"  
blueBase.y = 15  
physics.addBody(blueBase, "static", {bounce = 1, friction = 1})  
   
local diskGfx = { "redDisk.png", "blueDisk.png"}  
local allDisks = {} -- empty table for storing objects  
   
--Move this up here so we can add it to the red disks  
local function removeRedDisk(event)  
--Only remove the disk if the collision began and it's a blue bar  
 if(event.phase == "began" and event.other.color == "blue") then  
 event.target:removeSelf()  
 end  
end  
  
--Move this up here so we can add it to the blue disks  
local function removeBlueDisk(event)  
--Only remove the disk if the collision began and it's a red bar  
 if(event.phase == "began" and event.other.color == "red") then  
 event.target:removeSelf()  
 end  
end  
  
local function opposeGravity(event)  
 if (event.phase == "began" and randImage == "redDisk.png") then  
 event.target:applyLinearImpulse(9,9)  
 end  
end  
Runtime:addEventListener("enterFrame", opposeGravity)  
  
local function spawnRedDisk( event )  
 local phase = event.phase  
 if "ended" == phase then  
   
 --randImage = diskGfx[math.random( 1 )]  
 allDisks[#allDisks + 1] = display.newImage( "redDisk.png" )  
 local disk = allDisks[#allDisks]  
 disk.x = event.x; disk.y = event.y  
 disk.rotation = math.random( 1, 360 )  
 disk.xScale = 0.8; disk.yScale = 0.8  
 physics.addBody( disk, { bounce= 1, density=1, friction=1, radius= 50.0 } )  
 disk.linearDamping = 0.4  
 disk.angularDamping = 0.6  
 --If it's a blue disk, add the collision handler  
 --if (randImage == "blueDisk.png") then  
 -- disk.collision = removeBlueDisk  
 -- disk:addEventListener("collision",removeBlueDisk)  
 --end  
 --If it's a red disk, add the collision handler  
 if (randImage == "redDisk.png") then  
 disk.collision = removeRedDisk  
 disk:addEventListener("collision",removeRedDisk)  
 disk:applyForce(5,5,disk.x, disk.y)  
 end   
 end  
end  
bottomBG:addEventListener( "touch", spawnRedDisk ) -- touch the screen to create disks  
  
local function spawnBlueDisk( event )  
 local phase = event.phase  
 if "ended" == phase then  
   
 --randImage = diskGfx[math.random( 1 )]  
 allDisks[#allDisks + 1] = display.newImage( "blueDisk.png" )  
 local disk = allDisks[#allDisks]  
 disk.x = event.x; disk.y = event.y  
 disk.rotation = math.random( 1, 360 )  
 disk.xScale = 0.8; disk.yScale = 0.8  
 physics.addBody( disk, { bounce= 1, density=1, friction=1, radius= 50.0 } )  
 disk.linearDamping = 0.4  
 disk.angularDamping = 0.6  
  
  
 --If it's a blue disk, add the collision handler  
 if (randImage == "blueDisk.png") then  
 disk.collision = removeBlueDisk  
 disk:addEventListener("collision",removeBlueDisk)  
 end  
 --If it's a red disk, add the collision handler  
 --if (randImage == "redDisk.png") then  
 -- disk.collision = removeRedDisk  
 -- disk:addEventListener("collision",removeRedDisk)  
 --end   
 end  
end  
topBG:addEventListener( "touch", spawnBlueDisk ) -- touch the screen to create disks  

[import]uid: 7116 topic_id: 6873 reply_id: 24327[/import]

how about turning gravity off

[lua]physics.setGravity(0,0)[/lua]

and just applying forces to your disks yourself

[import]uid: 6645 topic_id: 6873 reply_id: 24337[/import]

I tried that, but I can’t figure out how to apply the force to the disks.

Would this be right?

  
local function opposeGravity(event)  
 if (event.phase == "began" and randImage == "redDisk") then  
 event.target:applyLinearImpulse(9,9)  
 end  
end  
Runtime:addEventListener("enterFrame", opposeGravity)  
  

(its from the code above) [import]uid: 7116 topic_id: 6873 reply_id: 24365[/import]

wouldnt it be something like (0,-9) to make it go straight upwards? (x force = 0, y force = -9)

also:
http://developer.anscamobile.com/forum/2011/01/24/balloon-or-object-has-reverse-gravity-force#comment-18376

gravity is a constant force. [import]uid: 6645 topic_id: 6873 reply_id: 24491[/import]

Yes. I understad that I need to make an opposite force for the red disks, but the problem I am having is that it doesn’t work. even when I set the gravity to (0,0) this function for apply force to the red disks doesn’t work, do you see anything right off the bat that is wrong with what have below?

local function opposeGravity(event) if (event.phase == "began" and randImage == "redDisk") then event.target:applyLinearImpulse(0,-9) end end Runtime:addEventListener("enterFrame", opposeGravity) [import]uid: 7116 topic_id: 6873 reply_id: 24501[/import]

[deleted] [import]uid: 6645 topic_id: 6873 reply_id: 24609[/import]

there’s no phase for enterFrame and there’s no target.

it’s just a global event running constantly. you’ll need to loop through your array of disks in your opposeGravity function and apply the relevant force to each.

and you probably want applyForce

[lua]local function opposeGravity(event)

for i,disk in ipairs(allDisks) do
if(disk.color==“red”) then
disk:applyForce(0,-9)
elseif (disk.color==“blue”) then
disk:applyForce(0,9)
end
end
end
Runtime:addEventListener(“enterFrame”, opposeGravity)[/lua]

and don’t forget to set disk.color in your spawn function in that case

also note when you remove a disk in your remove functions you need to remove it’s entry from the allDisks array [import]uid: 6645 topic_id: 6873 reply_id: 24610[/import]