Removing a object and replacing it with a new one.

Im having a problem, I have a coin that i want to remove when you touch it, and in its place a different coin to

appear. any help will be awesome!

heres what I have:

local function splitCoin( name )

  if name == “diamond” then

    print(name)

      display.remove( diamond )

When i make contact with the diamond, it wont remove, and i also cant figure out how to get another coin to spawn in the same place as the diamond.

What is diamond?  And how are you calling this function?  Please show a little more code.

Here is a little bit more

local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

local scoreTotal = 0

local scoreBoard

local highscore

local coin, mapleLeaf, diamond, C4

local coinTimer

local coins = {“coin”, “mapleLeaf”, “diamond”, “C4”}

local coinsScore = { 25, 50, 100, -1000 }

local genericCoin = coins

–local splitDiamond = {“DiamondB”, “DiamondT”}

math.randomseed( os.time() )

os.clock ()

local dubstep = audio.loadStream( “Ninja2.mp3” )

dubstep = audio.play( dubstep, { duration = 61500, onComplete = finished })

local coinsProp = {radius=20, density=1.0, friction=0.3, bounce=0.2, filter = {categoryBits = 2, maskBits = 1}}

local allCoins = {}

–[[

ego = require “ego”

saveFile = ego.saveFile

loadFile = ego.loadFile

–]]

  

local function splitCoin( name )

  if name == “diamond” then

    print(name)

      --display.remove( diamond )

      --display.newImage( “coin.png” )

  elseif name == “coin” then

    print(name)

  elseif name == “mapleLeaf” then

    print(name)

  elseif name == “C4” then

    print(name)

  end

end

local function coinTouchHandler( event )

  --if event.phase == “began” then

    local sounds = audio.loadSound(“METALonMETAL.mp3”);

    audio.play(sounds)

    print("begin "…event.target.name)

  --elseif event.phase == “ended” then

    scoreTotal = scoreTotal + event.target.coinValue

    scoreBoard.text = string.format("%d", scoreTotal ) 

    print(scoreTotal)

    --print(event.target)

    splitCoin( event.target.name )

  --end

end

local function createCoin( name, score )

  local genericCoin

  if name == “diamond” then

    genericCoin = display.newImage( “Diamond.png”, 90, 90 )

  elseif name == “coin” then

    genericCoin = display.newImage( “Coin.png”, 90, 90 )

  elseif name == “mapleLeaf” then

    genericCoin = display.newImage( “MapleLeaf.png”, 90, 90 )

  elseif name == “C4” then

    genericCoin = display.newImage( “C4.png”, 90, 90 )

end

  genericCoin.name = name

  genericCoin.x, genericCoin.y = 140, 550

  genericCoin.rotation = 100

  genericCoin.height = 50

  genericCoin.width = 50

  genericCoin.coinValue = score

  genericCoin:addEventListener( “touch”, coinTouchHandler )

  return genericCoin

end

local function getRandomCoin()

  local coinsProp = genericCoin[math.random(1, #genericCoin)]

  local coins = display.newImage(coinsProp.whole)

end 

local function shootObject(type)

  – logic for random coin

  local r = math.random( 1, 4 )

    --print®

  local object = createCoin(coins[r],coinsScore[r])

  – How I make the coin fly

  physics.addBody( object, { radius=20, density=1.5, friction=0.3, bounce=0.2, filter = {categoryBits = 2, maskBits = 1}} )

  physics.addBody( object, “dynamic” ) 

  --physics.setPreCollision = “passthru”

    local v = math.random( 340, 560 )

    local b = math.random( -65, 65 )

    local c = math.random( 20, 60 )

    object:setLinearVelocity( b, -v )

    object:applyTorque( -c )

end

There are several issues here.  I recommend reviewing the tutorials about scope.  

Also, the variable ‘diamond’ is never given any value.  That’s why display.remove(diamond) doesn’t work.  

no value? is this what you mean??

  coin = createCoin( “coin”, 25 )

  mapleLeaf = createCoin( “mapleLeaf”, 50 )

  diamond = createCoin( “diamond”, 100 )

  C4 = createCoin( “C4”, -1000 )

^ my coin values

Well i didn’t look over your code or anything but try this.

local function removeDiamond(event) if event.phase == "ended" then local removeIt = event.target display.remove(removeIt) end end diamond:addEventListener( "touch", removeDiamond )

–SonicX278

Thanks ill try that

I already have a event listener, and i tryed that, it didnt work, so i modified it a little bit, still didnt work… any ideas

What is diamond?  And how are you calling this function?  Please show a little more code.

Here is a little bit more

local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

local scoreTotal = 0

local scoreBoard

local highscore

local coin, mapleLeaf, diamond, C4

local coinTimer

local coins = {“coin”, “mapleLeaf”, “diamond”, “C4”}

local coinsScore = { 25, 50, 100, -1000 }

local genericCoin = coins

–local splitDiamond = {“DiamondB”, “DiamondT”}

math.randomseed( os.time() )

os.clock ()

local dubstep = audio.loadStream( “Ninja2.mp3” )

dubstep = audio.play( dubstep, { duration = 61500, onComplete = finished })

local coinsProp = {radius=20, density=1.0, friction=0.3, bounce=0.2, filter = {categoryBits = 2, maskBits = 1}}

local allCoins = {}

–[[

ego = require “ego”

saveFile = ego.saveFile

loadFile = ego.loadFile

–]]

  

local function splitCoin( name )

  if name == “diamond” then

    print(name)

      --display.remove( diamond )

      --display.newImage( “coin.png” )

  elseif name == “coin” then

    print(name)

  elseif name == “mapleLeaf” then

    print(name)

  elseif name == “C4” then

    print(name)

  end

end

local function coinTouchHandler( event )

  --if event.phase == “began” then

    local sounds = audio.loadSound(“METALonMETAL.mp3”);

    audio.play(sounds)

    print("begin "…event.target.name)

  --elseif event.phase == “ended” then

    scoreTotal = scoreTotal + event.target.coinValue

    scoreBoard.text = string.format("%d", scoreTotal ) 

    print(scoreTotal)

    --print(event.target)

    splitCoin( event.target.name )

  --end

end

local function createCoin( name, score )

  local genericCoin

  if name == “diamond” then

    genericCoin = display.newImage( “Diamond.png”, 90, 90 )

  elseif name == “coin” then

    genericCoin = display.newImage( “Coin.png”, 90, 90 )

  elseif name == “mapleLeaf” then

    genericCoin = display.newImage( “MapleLeaf.png”, 90, 90 )

  elseif name == “C4” then

    genericCoin = display.newImage( “C4.png”, 90, 90 )

end

  genericCoin.name = name

  genericCoin.x, genericCoin.y = 140, 550

  genericCoin.rotation = 100

  genericCoin.height = 50

  genericCoin.width = 50

  genericCoin.coinValue = score

  genericCoin:addEventListener( “touch”, coinTouchHandler )

  return genericCoin

end

local function getRandomCoin()

  local coinsProp = genericCoin[math.random(1, #genericCoin)]

  local coins = display.newImage(coinsProp.whole)

end 

local function shootObject(type)

  – logic for random coin

  local r = math.random( 1, 4 )

    --print®

  local object = createCoin(coins[r],coinsScore[r])

  – How I make the coin fly

  physics.addBody( object, { radius=20, density=1.5, friction=0.3, bounce=0.2, filter = {categoryBits = 2, maskBits = 1}} )

  physics.addBody( object, “dynamic” ) 

  --physics.setPreCollision = “passthru”

    local v = math.random( 340, 560 )

    local b = math.random( -65, 65 )

    local c = math.random( 20, 60 )

    object:setLinearVelocity( b, -v )

    object:applyTorque( -c )

end

There are several issues here.  I recommend reviewing the tutorials about scope.  

Also, the variable ‘diamond’ is never given any value.  That’s why display.remove(diamond) doesn’t work.  

no value? is this what you mean??

  coin = createCoin( “coin”, 25 )

  mapleLeaf = createCoin( “mapleLeaf”, 50 )

  diamond = createCoin( “diamond”, 100 )

  C4 = createCoin( “C4”, -1000 )

^ my coin values

Well i didn’t look over your code or anything but try this.

local function removeDiamond(event) if event.phase == "ended" then local removeIt = event.target display.remove(removeIt) end end diamond:addEventListener( "touch", removeDiamond )

–SonicX278

Thanks ill try that

I already have a event listener, and i tryed that, it didnt work, so i modified it a little bit, still didnt work… any ideas