Hi, I’m very new to everything, but I’ve mainly been having a ton of trouble with tables and having enemies (in my case, fighter jets) appear on screen. I’m not exactly sure what I’ve done wrong, but I’m sure my code is a mess because I’ve been trying many different things to make it work. I really need help to understand what it is I’m doing wrong, and what method should I be using. Thank you for any help!
Edit: I’m creating a physics game very similar to flappy bird where I have a UFO moving along a Parallax background and dodging incoming fighter jets from the right side of the screen. I initially had everything operating correctly except for collision (I can’t seem to make the collision function take the player to the “menu” screen). But I realized I should be utilizing tables to create the fighter jet enemies instead of creating each individual jet since they are all the same png file. I tried to do this, but I can’t seem to figure out how to implement tables for my fighter jet enemies. They just don’t appear at all when I try this method.
local composer = require( "composer" ) local scene = composer.newScene() local physics = require "physics" physics.start() -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- local background local city1 local city2 local city3 local city4 local ufo = display.newImage("ufo.png") ufo.x = 100 ufo.y = 100 ufo:scale( .1, .1 ) physics.addBody(ufo, "dynamic", {density=1, bounce=0.1, friction=2, radius=12} ) local fighter = display.newImage("fighter.png") local fightersTable = {} local score = 0 -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- function activateUfo(self,event) self:applyForce(0, -10, self.x, self.y) end function touchScreen(event) if event.phase == "began" then ufo.enterFrame = activateUfo Runtime:addEventListener("enterFrame", ufo) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame", ufo) end end Runtime:addEventListener("touch", touchScreen) function activateUfo(self,event) self:applyForce(0, -10, self.x, self.y) end function scrollCity(self,event) if self.x \< -800 then self.x = 900 else self.x = self.x - self.speed end end local function createFighters() local newFighter = display.newImage("fighter.png") table.insert( fightersTable, newFighter) physics.addBody(fighter, "static", {density=1, bounce=0.1, friction=2, radius=12} ) newFighter.myName = "fighter" local whereFrom = math.random(3) if ( whereFrom == 1 ) then fighter.x = 500 newFighter.y = math.random(50, 300) newFighter:scale( .08, .08 ) newFighter.speed = math.random(2,6) newFighter.initY = fighter.y newFighter.amp = math.random(20,100) newFighter.angle = math.random(1,360) newFighter:setLinearVelocity( math.random(500, 300)) elseif ( whereFrom == 2 ) then fighter1.x = 500 newFighter.y = math.random(200, 300) newFighter:scale( .08, .08 ) newFighter.speed = math.random(6,9) newFighter.initY = fighter1.y newFighter.amp = math.random(20,100) newFighter.angle = math.random(1,360) newFighter:setLinearVelocity( math.random(500, 100)) elseif ( whereFrom == 3 ) then newFighter.x = 500 newFighter.y = math.random(100, 200) newFighter:scale( .08, .08 ) newFighter.speed = math.random(3,7) newFighter.initY = fighter.y newFighter.amp = math.random(20,100) newFighter.angle = math.random(1,360) newFighter:setLinearVelocity( math.random(500, 200)) end end function onCollision(event) if event.phase == "began" then end end local function updateText() scoreText.text = "Score: " .. score end -- create() function scene:create( event ) local sceneGroup = self.view scoreText = display.newText( "Score: " .. score, 200, 80, native.systemFont, 36 ) local background = display.newImageRect( "BG.png", 900, 480 ) background.x = display.contentCenterX background.y = display.contentCenterY local city1 = display.newImageRect( "city1.png", 900, 480 ) city1.x = display.contentCenterX city1.y = display.contentCenterY + 110 city1.speed = 1 local city2 = display.newImageRect( "city1.png", 900, 480 ) city2.x = display.contentCenterX + 900 city2.y = display.contentCenterY + 110 city2.speed = 1 local city3 = display.newImageRect( "city2.png", 900, 480 ) city3.x = display.contentCenterX city3.y = display.contentCenterY + 5 city3.speed = 2 local city4 = display.newImageRect( "city2.png", 900, 480 ) city4.x = display.contentCenterX + 900 city4.y = display.contentCenterY + 5 city4.speed = 2 ufo = display.newImage("ufo.png") ufo.x = 50 ufo.y = 50 ufo:scale( .1, .1 ) physics.addBody(ufo, "dynamic", {density=1, bounce=0.1, friction=2, radius=12} ) ufo.collision = onCollision city1.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city1) city2.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city2) city3.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city3) city4.enterFrame = scrollCity Runtime:addEventListener("enterFrame", city4) fighter.enterFrame = moveFighters Runtime:addEventListener("enterFrame", fighter) Runtime:addEventListener("collision", onCollision) -- Code here runs when the scene is first created but has not yet appeared on screen local function gameLoop() createFighters() end end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene
[lua]