Need urgent help with code

Hey,

I need help moving an object from off the screen to the middle of the screen for my game.

The player controls the spaceship and has to avoid getting hit by asteroids by shooting them. If the player comes into contact with an asteroid, they lose a life and if the player loses all three lives they lose the game. With each asteroid destroyed, the player gets more points. The aim of the game is to get the most points without losing all three lives.

The spaceship doesn’t move around but can do a 360. I haven’t coded that part just yet.  The problem is on line 91 of game.lua. To send the whole folder, its in a 7.zip folder

Thanks,

Nick

link to project

https://www.mediafire.com/?5uc724af7t7djl8 (it was 3.9mb so i coudnt attach it)

Try replacing your game.lua code with this.  You have much more work to do still…  :huh:

--include Corona's "physics" library local physics = require "physics" physics.start(); physics.pause() local screenW = display.contentWidth local screenH = display.contentHeight local numberOfLives = 3 local bulletSpeed = 0.35 local badGuyMovementSpeed = 1500 local badGuyCreationSpeed = 1000 local lives = {} local badGuy = {} local badGuyCounter = 1 local score = 0 local txt\_score local background, tmr\_createBadGuy, bullet, txt\_score local tmr\_createBadGuy local badGuyCounter = 1 local createBadGuy = nil --Display group for UI objects like the score local uiGroup = display.newGroup() -- Use the require function to include the Corona "composer" module so we can create a new scene. local composer = require( "composer" ) --Use composer to create a new scene. Use local sence local scene = composer.newScene() -- This function is a call-back. When the user taps the button, this function will get called. local function handleButtonEvent( event ) if ( "ended" == event.phase ) then composer.gotoScene("menu", { effect = "crossFade", time = 333 }) end end function scene:create( event ) local sceneGroup = self.view params = event.params --Adding background to be shown on game. (x,y is position of backgeound to be centered) local background = display.newImage("background.png") background.x = display.contentCenterX background.y = display.contentCenterY sceneGroup:insert( background ) -- Place our spaceship in the center of screen local spaceship = display.newImageRect("spaceship.png",50,70) spaceship.x = display.contentCenterX spaceship.y = display.contentCenterY txt\_score = display.newText("Score: "..score,200,15 ,native.systemFont,18) txt\_score.x = 530 for i=1,numberOfLives do lives[i] = display.newImageRect("heart.png",30,24) lives[i].x = i\*40-20 lives[i].y = 18 end --This function will create our bad guy function createBadGuy() -- Determine the enemies starting position local startingPosition = math.random(1,4) local startingX = nil local startingY = nil local vx, vy = 0, 0 if(startingPosition == 1) then -- Send bad guy from left side of the screen startingX = -10 startingY = math.random(0,screenH) vx, vy = 150, -30 elseif(startingPosition == 2) then -- Send bad guy from right side of the screen startingX = screenW + 10 startingY = math.random(0,screenH) vx, vy = -150, -30 elseif(startingPosition == 3) then -- Send bad guy from the top of the screen startingX = math.random(0,screenW) startingY = -10 vx, vy = -30, 150 else -- Send bad guy from the bototm of the screen startingX = math.random(0,screenW) startingY = screenH + 10 vx, vy = 30, -150 end --Start the bad guy according to starting position badGuy[badGuyCounter] = display.newImageRect("alien.png",34,34) badGuy[badGuyCounter].x = startingX badGuy[badGuyCounter].y = startingY physics.addBody( badGuy[badGuyCounter], "dynamic", { isSensor=true, radius=17} ) badGuy[badGuyCounter].gravityScale = 0 badGuy[badGuyCounter].name = "badGuy" badGuy[badGuyCounter]:setLinearVelocity(vx, vy) sceneGroup:insert( badGuy[badGuyCounter] ) print("You're gonna need something to clean these up at some point or you will run out of memory...") end end function scene:show( event ) local group = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then --Start asteroids... physics.start() timer.performWithDelay(500, createBadGuy, -1) end end --Add event listeners for all of the scene events we want to get called for. scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --Finally, we return the scene that we just defined so composer can make use of it. return scene

Try replacing your game.lua code with this.  You have much more work to do still…  :huh:

--include Corona's "physics" library local physics = require "physics" physics.start(); physics.pause() local screenW = display.contentWidth local screenH = display.contentHeight local numberOfLives = 3 local bulletSpeed = 0.35 local badGuyMovementSpeed = 1500 local badGuyCreationSpeed = 1000 local lives = {} local badGuy = {} local badGuyCounter = 1 local score = 0 local txt\_score local background, tmr\_createBadGuy, bullet, txt\_score local tmr\_createBadGuy local badGuyCounter = 1 local createBadGuy = nil --Display group for UI objects like the score local uiGroup = display.newGroup() -- Use the require function to include the Corona "composer" module so we can create a new scene. local composer = require( "composer" ) --Use composer to create a new scene. Use local sence local scene = composer.newScene() -- This function is a call-back. When the user taps the button, this function will get called. local function handleButtonEvent( event ) if ( "ended" == event.phase ) then composer.gotoScene("menu", { effect = "crossFade", time = 333 }) end end function scene:create( event ) local sceneGroup = self.view params = event.params --Adding background to be shown on game. (x,y is position of backgeound to be centered) local background = display.newImage("background.png") background.x = display.contentCenterX background.y = display.contentCenterY sceneGroup:insert( background ) -- Place our spaceship in the center of screen local spaceship = display.newImageRect("spaceship.png",50,70) spaceship.x = display.contentCenterX spaceship.y = display.contentCenterY txt\_score = display.newText("Score: "..score,200,15 ,native.systemFont,18) txt\_score.x = 530 for i=1,numberOfLives do lives[i] = display.newImageRect("heart.png",30,24) lives[i].x = i\*40-20 lives[i].y = 18 end --This function will create our bad guy function createBadGuy() -- Determine the enemies starting position local startingPosition = math.random(1,4) local startingX = nil local startingY = nil local vx, vy = 0, 0 if(startingPosition == 1) then -- Send bad guy from left side of the screen startingX = -10 startingY = math.random(0,screenH) vx, vy = 150, -30 elseif(startingPosition == 2) then -- Send bad guy from right side of the screen startingX = screenW + 10 startingY = math.random(0,screenH) vx, vy = -150, -30 elseif(startingPosition == 3) then -- Send bad guy from the top of the screen startingX = math.random(0,screenW) startingY = -10 vx, vy = -30, 150 else -- Send bad guy from the bototm of the screen startingX = math.random(0,screenW) startingY = screenH + 10 vx, vy = 30, -150 end --Start the bad guy according to starting position badGuy[badGuyCounter] = display.newImageRect("alien.png",34,34) badGuy[badGuyCounter].x = startingX badGuy[badGuyCounter].y = startingY physics.addBody( badGuy[badGuyCounter], "dynamic", { isSensor=true, radius=17} ) badGuy[badGuyCounter].gravityScale = 0 badGuy[badGuyCounter].name = "badGuy" badGuy[badGuyCounter]:setLinearVelocity(vx, vy) sceneGroup:insert( badGuy[badGuyCounter] ) print("You're gonna need something to clean these up at some point or you will run out of memory...") end end function scene:show( event ) local group = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then --Start asteroids... physics.start() timer.performWithDelay(500, createBadGuy, -1) end end --Add event listeners for all of the scene events we want to get called for. scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --Finally, we return the scene that we just defined so composer can make use of it. return scene