Hi.
I’m trying to make my first game. I want “things1” to disappear after collision with “KittyKat”, but I have no idea how to do that. I don’t know what part of my code will you need, so I’m posting everything. I’m sorry for my English. It’s bad because I’m from Poland.
pls help
local physics = require ( "physics" ) physics.start() physics.setGravity( 0, 0 ) -- Seed the random number generator math.randomseed( os.time() ) -- Configure image sheet local sheetOptions = { frames = { { -- 1) ZosiaSosia x = 0, y = 0, width = 119, height = 130 }, { -- 2) Fiona x = 0, y = 130, width = 111, height = 120 }, { -- 3) Goblin x = 0, y = 251, width = 111, height = 120 }, { -- 4) szczur x = 0, y = 373, width = 70, height = 61 }, { -- 5) miska x = 0, y = 436, width = 60, height = 35 }, } } local objectSheet = graphics.newImageSheet( "Things0.png", sheetOptions ) -- Initialize variables local lives = 3 local score = 0 local died = false local thingsTable = {} local KittyKat local gameLoopTimer local livesText local scoreText -- Set up display groups local backGroup = display.newGroup() -- Display group for the background image local mainGroup = display.newGroup() -- Display group for the things local uiGroup = display.newGroup() -- Display group UI objects like the score -- Load the background local background = display.newImageRect( backGroup, "background.PNG", 800, 1400 ) background.x = display.contentCenterX background.y = display.contentCenterY -- Load the KittyKat KittyKat = display.newImageRect( mainGroup, "KittyKat.png" , 119, 130 ) KittyKat.x = display.contentCenterX KittyKat.y = display.contentHeight - 100 physics.addBody( KittyKat, { radius=30, isSensor=true } ) KittyKat.myName = "KittyKat" -- Display lives and score livesText = display.newText( uiGroup, "Lives: " .. lives, 200, 80, native.systemFont, 36 ) scoreText = display.newText( uiGroup, "Score: " .. score, 400, 80, native.systemFont, 36 ) -- Hide the status bar display.setStatusBar( display.HiddenStatusBar ) local function updateText() livesText.text = "Lives: " .. lives scoreText.text = "Score: " .. score end -- Load the things local function createThings() -- insert ZosieSosie local newThings = display.newImageRect( mainGroup, objectSheet, 1, 119, 130 ) table.insert( thingsTable, newThings ) physics.addBody( newThings, "dynamic", { radius=40, bounce=0.8 } ) newThings.myName = "things1" local whereFrom = math.random( 3 ) if ( whereFrom == 1 ) then -- From the left newThings.x = -60 newThings.y = math.random( 5000 ) newThings:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) ) elseif ( whereFrom == 2 ) then -- From the top newThings.x = math.random( 5000 ) newThings.y = -60 newThings:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) ) elseif ( whereFrom == 3 ) then -- From the right newThings.x = display.contentWidth + 60 newThings.y = math.random( 5000 ) newThings:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) ) end -- insert Fiona local newThings = display.newImageRect( mainGroup, objectSheet, 2, 111, 120 ) table.insert( thingsTable, newThings ) physics.addBody( newThings, "dynamic", { radius=40, bounce=0.8 } ) newThings.myName = "things2" local whereFrom = math.random( 3 ) if ( whereFrom == 1 ) then -- From the left newThings.x = -60 newThings.y = math.random( 3000 ) newThings:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) ) elseif ( whereFrom == 2 ) then -- From the top newThings.x = math.random( 4000 ) newThings.y = -60 newThings:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) ) elseif ( whereFrom == 3 ) then -- From the right newThings.x = display.contentWidth + 60 newThings.y = math.random( 3000 ) newThings:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) ) end --insert Goblin local newThings = display.newImageRect( mainGroup, objectSheet, 3, 111, 120 ) table.insert( thingsTable, newThings ) physics.addBody( newThings, "dynamic", { radius=40, bounce=0.8 } ) newThings.myName = "things2" local whereFrom = math.random( 3 ) if ( whereFrom == 1 ) then -- From the left newThings.x = -60 newThings.y = math.random( 3000 ) newThings:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) ) elseif ( whereFrom == 2 ) then -- From the top newThings.x = math.random( 3000 ) newThings.y = -60 newThings:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) ) elseif ( whereFrom == 3 ) then -- From the right newThings.x = display.contentWidth + 60 newThings.y = math.random( 3000 ) newThings:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) ) end --insert szczur local newThings = display.newImageRect( mainGroup, objectSheet, 4, 70, 61 ) table.insert( thingsTable, newThings ) physics.addBody( newThings, "dynamic", { radius=40, bounce=0.8 } ) newThings.myName = "things1" local whereFrom = math.random( 3 ) if ( whereFrom == 1 ) then -- From the left newThings.x = -60 newThings.y = math.random( 3000 ) newThings:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) ) elseif ( whereFrom == 2 ) then -- From the top newThings.x = math.random( 3000 ) newThings.y = -60 newThings:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) ) elseif ( whereFrom == 3 ) then -- From the right newThings.x = display.contentWidth + 60 newThings.y = math.random( 3000 ) newThings:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) ) end --insert miska local newThings = display.newImageRect( mainGroup, objectSheet, 5, 60, 35 ) table.insert( thingsTable, newThings ) physics.addBody( newThings, "dynamic", { radius=40, bounce=0.8 } ) newThings.myName = "things1" local whereFrom = math.random( 3 ) if ( whereFrom == 1 ) then -- From the left newThings.x = -60 newThings.y = math.random( 3000 ) newThings:setLinearVelocity( math.random( 40,120 ), math.random( 20,60 ) ) elseif ( whereFrom == 2 ) then -- From the top newThings.x = math.random( 3000 ) newThings.y = -60 newThings:setLinearVelocity( math.random( -40,40 ), math.random( 40,120 ) ) elseif ( whereFrom == 3 ) then -- From the right newThings.x = display.contentWidth + 60 newThings.y = math.random( 3000 ) newThings:setLinearVelocity( math.random( -120,-40 ), math.random( 20,60 ) ) end end -- Moving KittyKat local function dragKittyKat( event ) local KittyKat = event.target local phase = event.phase if ( "began" == phase ) then -- Set touch focus on the KittyKat display.currentStage:setFocus( KittyKat ) -- Store initial offset position KittyKat.touchOffsetX = event.x - KittyKat.x KittyKat.touchOffsetY = event.y - KittyKat.y elseif ( "moved" == phase ) then -- Move the KittyKat to the new touch position KittyKat.x = event.x - KittyKat.touchOffsetX KittyKat.y = event.y - KittyKat.touchOffsetY elseif ( "ended" == phase or "cancelled" == phase ) then -- Release touch focus on the KittyKat display.currentStage:setFocus( nil ) end return true -- Prevents touch propagation to underlying objects end KittyKat:addEventListener( "touch", dragKittyKat ) local function gameLoop() --Create new things createThings() --Remove things which have drifted for i = #thingsTable, 1, -1 do local thisThings = thingsTable[i] if ( thisThings.x \< -100 or thisThings.x \> display.contentWidth + 100 or thisThings.y \< -100 or thisThings.y \> display. contentHeight + 100 ) then display.remove( thisThings ) table.remove( thingsTable, i ) end end end gameLoopTimer = timer.performWithDelay( 2000, gameLoop, 0 ) local function restoreKittyKat() KittyKat.isBodyActive = false KittyKat:setLinearVelocity( 0, 0 ) KittyKat.x = display.contentCenterX KittyKat.y = display.contentHeight - 100 -- Fade in the KittyKat transition.to( KittyKat, {alpha=1, time=4000, onComplete = function() KittyKat.isBodyActive = true died = false end } ) end local function onCollision( event ) if ( event.phase == "began" ) then local obj1 = event.object1 local obj2 = event.object2 if ( ( obj1.myName == "KittyKat" and obj2.myName == "things2" ) or ( obj1.myName == "things2" and obj2.myName == "KittyKat" ) ) then if ( died == false ) then died = true -- Update lives lives = lives - 1 livesText.text = "Lives: " .. lives if ( lives == 0 ) then display.remove( KittyKat ) else KittyKat.alpha = 0 timer.performWithDelay( 500, restoreKittyKat ) end end end end if ( event.phase == "began" ) then local obj1 = event.object1 local obj2 = event.object2 if ( ( obj1.myName == "KittyKat" and obj2.myName == "things1" ) or ( obj1.myName == "things1" and obj2.myName == "KittyKat" ) ) then -- Increase score score = score + 100 scoreText.text = "Score: " .. score end end end Runtime:addEventListener( "collision", onCollision )