I am 11 years old and a novice at Corona.
I have been working to make it so you can have an airplane that moves towards your touch and can fire bullets quickly. I got it to work, but it only fires when your touch is moving and it fires so rapidly it seems more like a flame thrower than a machine gun. How do I get it work so that it fires once each half-second?
Here’s all my code:
[code]
– Activate the physics model
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0 )
– Create the background for plane motion purposes
local background = display.newRect( 85, 0, 480, 320 )
background:setFillColor( 0, 0, 0 )
– Create the plane and give it a filter so it doesn’t collide with the bullets it’s firing
local planeCollisionFilter = { categoryBits = 2, maskBits = 0 }
local plane = display.newImage( “plane.png” )
plane.x = display.contentWidth / 2; plane.y = display.contentHeight / 1.25
plane.width = 90; plane.height = 70
physics.addBody( plane, { filter = planeCollisionFilter } )
– Create a function so the plane can be moved around
local function movePlane(event)
distanceX = event.x - plane.x
distanceY = event.y - plane.y
plane:setLinearVelocity( distanceX, distanceY )
end
– Give the background a touch listener so that the plane won’t move when you hit the ‘fire’ button
background:addEventListener( “touch”, movePlane )
– Create a collision filter for the bullets so they don’t collide with the plane
local bulletCollisionFilter = { categoryBits = 1, maskBits = 1 }
– Create a ‘fire’ button
local button = display.newCircle( 35, 285, 25 )
button:setFillColor( 155, 0, 0 )
– Create a function to fire the bullets
local function spawnBullets(event)
local leftBullet = display.newRect( 0, 0, 5, 15 )
leftBullet:setFillColor( 255, 255, 0 )
leftBullet.x = plane.x - 15
leftBullet.y = plane.y - 20
physics.addBody( leftBullet, “kinematic”, { filter = bulletCollisionFilter } )
leftBullet:setLinearVelocity( math.random( -10, 10 ), -200 )
local rightBullet = display.newRect( 0, 0, 5, 15 )
rightBullet:setFillColor( 255, 255, 0 )
rightBullet.x = plane.x + 15
rightBullet.y = plane.y - 20
physics.addBody( rightBullet, “kinematic”, { filter = bulletCollisionFilter } )
rightBullet:setLinearVelocity( math.random( -10, 10 ), -200 )
end
– Give the button a touch listener to fire the bullets
button:addEventListener( “touch”, spawnBullets ) [import]uid: 82408 topic_id: 17037 reply_id: 317037[/import]