Hi, I know I am over thinking this, but I just wanted to know how to make a display object move automatically across the screen. Basically like the video below. My game is very similiar I just want the spawned objects to move.
You have three basic choices:
- Physics
- Transitions
- Roll-your-own ( usually enterFrame or timer) based.
Please note that, you haven’t really given folks enough info to answer your question, and it is unlikely anyone will have or share ready-made code. That said, it isn’t hard. I suggest examining the many examples that come with Corona, the guides you’ll find on this site, the blogs (lots of good examples), the Corona Geek Show, and of course the docs:
- Examples: http://coronalabs.com/resources/tutorials/sample-code/
- Guides:
- Blogs: http://coronalabs.com/blog/
- Corona Geek b http://coronageek.com/
-
Docs: http://docs.coronalabs.com/daily/api/
- Physics - http://docs.coronalabs.com/daily/api/library/physics/index.html
- Transitions - http://docs.coronalabs.com/daily/api/library/transition/index.html
- enterFrame - http://docs.coronalabs.com/daily/api/event/enterFrame/index.html
- timer - http://docs.coronalabs.com/daily/api/library/timer/index.html
Sorry for the lack of detail , thanks for the info, I decided to go with object:appliedforce()-This seems to work well.My next goal is to spawn my now horizontal moving enemies from random points along the x and y axis like the video above. What would you personally suggest to be the best way to do this? I know that this involves the math.random feature it is just the implementation that throw me off.
Here is the relevant code so far- If you could point in the right direction that would help.
local objects = {} local tPrevious = system.getTimer( ) local speedFactor = 1 --------------------- local Player = require( "Player" ) local Spawn = require( "Spawn" ) local world, player1,spawn,timerId local composer = require( "composer" ) local scene = composer.newScene() local physics = require "physics" physics.start() physics.setGravity( 0, 0 ) physics.setScale( 60 ) physics.setDrawMode( "hybrid" ) timerId = timer.performWithDelay( 1000,spawn,-1 ) --------------- FUNCTION VARIABLES --------------- -- Variables to hold the function pointers. -- This is only to have one place to show them off. local loadScores local saveScores local createPlayer local randomSpeed local calculateNewVelocity local showMenu local startGame local createMenu local gameOver local coerceOnScreen local onTouch local spawn local gameSpecial local onCollision local animate local randomSpeed --Hide the status bar. display.setStatusBar( display.HiddenStatusBar) local function onCollision( event ) --------Collision Event if ( event.phase == "began" ) then composer.gotoScene( "menu" ) end end -- Actual Scene function scene:create( event ) print( "Hello" ) local sceneGroup = self.view ----Setting Up Reset Values local gameIsOver = false speedFactor = 1 local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight ) -----Sets Background to White background:setFillColor( 1 ) background.x = display.contentCenterX background.y = display.contentCenterY sceneGroup:insert( background ) ---------------------------------------------------------------------Add as score label local score = 0 local scoreLabel = display.newText( score, 0,0, native.systemFontBold, 24) scoreLabel.x = display.viewableContentWidth / 1.5 scoreLabel.y = display.viewableContentHeight / 15 scoreLabel :setTextColor(0,0,0) scoreLabel.isVisible = true --------------------------------------------------------------------Collision Detection--------- local function realCollision ( event ) if "began" == event.phase then local food local food2 if event.object2.objectType == "food" then food = event.object2 ---INCREASE SCORE print ( " YOU GAIN POINTS ") score = score + 1 scoreLabel.text = tostring(score) timer.performWithDelay(100, function() display.remove( food ) end ) end return true end end -- Start listening to collision events. Runtime:addEventListener ( "collision", realCollision ) world = display.newGroup() ----------------------------------------------------Insert Player1 local player1 = Player.new(50, 50, physics) player1.objectType = "player1" world:insert(player1) -------------------------------------------------------Enemies------------------ local spawn = Spawn.new(50, 50, physics) spawn.objectType = "food" spawn.isFixedRotation = true spawn.speedFactor = 1 spawn.isBullet = true spawn:applyForce( 1, 0, spawn.x, spawn.y ) world:insert(spawn)
You have three basic choices:
- Physics
- Transitions
- Roll-your-own ( usually enterFrame or timer) based.
Please note that, you haven’t really given folks enough info to answer your question, and it is unlikely anyone will have or share ready-made code. That said, it isn’t hard. I suggest examining the many examples that come with Corona, the guides you’ll find on this site, the blogs (lots of good examples), the Corona Geek Show, and of course the docs:
- Examples: http://coronalabs.com/resources/tutorials/sample-code/
- Guides:
- Blogs: http://coronalabs.com/blog/
- Corona Geek b http://coronageek.com/
-
Docs: http://docs.coronalabs.com/daily/api/
- Physics - http://docs.coronalabs.com/daily/api/library/physics/index.html
- Transitions - http://docs.coronalabs.com/daily/api/library/transition/index.html
- enterFrame - http://docs.coronalabs.com/daily/api/event/enterFrame/index.html
- timer - http://docs.coronalabs.com/daily/api/library/timer/index.html
Sorry for the lack of detail , thanks for the info, I decided to go with object:appliedforce()-This seems to work well.My next goal is to spawn my now horizontal moving enemies from random points along the x and y axis like the video above. What would you personally suggest to be the best way to do this? I know that this involves the math.random feature it is just the implementation that throw me off.
Here is the relevant code so far- If you could point in the right direction that would help.
local objects = {} local tPrevious = system.getTimer( ) local speedFactor = 1 --------------------- local Player = require( "Player" ) local Spawn = require( "Spawn" ) local world, player1,spawn,timerId local composer = require( "composer" ) local scene = composer.newScene() local physics = require "physics" physics.start() physics.setGravity( 0, 0 ) physics.setScale( 60 ) physics.setDrawMode( "hybrid" ) timerId = timer.performWithDelay( 1000,spawn,-1 ) --------------- FUNCTION VARIABLES --------------- -- Variables to hold the function pointers. -- This is only to have one place to show them off. local loadScores local saveScores local createPlayer local randomSpeed local calculateNewVelocity local showMenu local startGame local createMenu local gameOver local coerceOnScreen local onTouch local spawn local gameSpecial local onCollision local animate local randomSpeed --Hide the status bar. display.setStatusBar( display.HiddenStatusBar) local function onCollision( event ) --------Collision Event if ( event.phase == "began" ) then composer.gotoScene( "menu" ) end end -- Actual Scene function scene:create( event ) print( "Hello" ) local sceneGroup = self.view ----Setting Up Reset Values local gameIsOver = false speedFactor = 1 local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight ) -----Sets Background to White background:setFillColor( 1 ) background.x = display.contentCenterX background.y = display.contentCenterY sceneGroup:insert( background ) ---------------------------------------------------------------------Add as score label local score = 0 local scoreLabel = display.newText( score, 0,0, native.systemFontBold, 24) scoreLabel.x = display.viewableContentWidth / 1.5 scoreLabel.y = display.viewableContentHeight / 15 scoreLabel :setTextColor(0,0,0) scoreLabel.isVisible = true --------------------------------------------------------------------Collision Detection--------- local function realCollision ( event ) if "began" == event.phase then local food local food2 if event.object2.objectType == "food" then food = event.object2 ---INCREASE SCORE print ( " YOU GAIN POINTS ") score = score + 1 scoreLabel.text = tostring(score) timer.performWithDelay(100, function() display.remove( food ) end ) end return true end end -- Start listening to collision events. Runtime:addEventListener ( "collision", realCollision ) world = display.newGroup() ----------------------------------------------------Insert Player1 local player1 = Player.new(50, 50, physics) player1.objectType = "player1" world:insert(player1) -------------------------------------------------------Enemies------------------ local spawn = Spawn.new(50, 50, physics) spawn.objectType = "food" spawn.isFixedRotation = true spawn.speedFactor = 1 spawn.isBullet = true spawn:applyForce( 1, 0, spawn.x, spawn.y ) world:insert(spawn)