in the game you make crates and destroy them by shooting a cannonball at then
-i can make crates
-i can destroy them by shooting a cannon ball
-i can rotate the cannon
THE PROBLEM:
i want the force applied on the cannonball to change based on the cannons rotation
p.s.: i updated the code a little
i didn’t post the whole code the first time because it is a little long
[lua]local physics = require(“physics”)
local gameUI = require(“gameUI”)
physics.start()
physics.setGravity( 0, 0 )
–the sky
local sky = display.newImage( “sky.png” )
–the boundries
–bottom
local ground = display.newImage( “ground.png” )
ground.x = 300
ground.y = 1060
physics.addBody( ground )
ground.bodyType = “static”
–top
local ground4 = display.newImage( “ground.png” )
ground4.x = 300
ground4.y = -40
physics.addBody( ground4 )
ground4.bodyType = “static”
–right side
local ground3 = display.newImage( “ground.png” )
ground3.x = 720
ground3.y = 500
physics.addBody( ground3 )
ground3.bodyType = “static”
ground3.rotation = 90
local launch = display.newImage( “down.png” )
launch.x = 125
launch.y = 180
local editButton = display.newImage( “edit.png”, 100, 20)
local Reset=display.newImage(“reset.png”,150, 30)
local boxGenerator = display.newImage( “crate.png”, 100, 960 )
local wallmaker = display.newImage( “wall.png”, 125, 760 )
– Create ball
local function dragBody( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
– Make body type temporarily “kinematic” (to avoid gravitional forces)
event.target.bodyType = “kinematic”
– Stop current motion, if any
event.target:setLinearVelocity( 0, 0 )
event.target.angularVelocity = 0
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
– Switch body type back to “dynamic”, unless we’ve marked this sprite as a platform
if ( not event.target.isPlatform ) then
event.target.bodyType = “dynamic”
end
end
end
– Stop further propagation of touch event!
return true
end
function moveCircle( event )
local phase = event.phase
if “ended” == phase then
local function onLocalPostCollision(self, event)
if ( event.force > 55 ) then
boom = display.newImage(“boom.png”)
boom.x = self.x
boom.y = self.y
transition.to( boom, { time=1500, alpha=0, xScale=1.5, yScale=1.5 } )
self:removeSelf()
print( "postCollision force: " … event.force … ", friction: " … event.friction )
end
end
local crate = display.newImage( “crate.png” )
physics.addBody( crate, { density = 5,friction=0.5, bounce=0.3, filter=finish1CollisionFilter } )
crate.x = event.x
crate.y = event.y
crate:addEventListener( “touch”, dragBody )
crate.postCollision = onLocalPostCollision
crate:addEventListener( “postCollision”, crate )
end
end
boxGenerator:addEventListener( “touch”, moveCircle )
function wallMaker( event )
local phase = event.phase
if “ended” == phase then
local function onLocalPostCollision(self, event)
if ( event.force > 35 ) then
boom = display.newImage(“boom.png”)
boom.x = self.x
boom.y = self.y
transition.to( boom, { time=1500, alpha=0, xScale=1.5, yScale=1.5 } )
self:removeSelf()
print( "postCollision force: " … event.force … ", friction: " … event.friction )
end
end
local wall = display.newImage( “wall.png” )
physics.addBody( wall, { density=3.0, friction=0.5, bounce=0.3 } )
wall.x = event.x
wall.y = event.y
wall:addEventListener( “touch”, dragBody )
wall.postCollision = onLocalPostCollision
wall:addEventListener( “postCollision”, wall )
end
end
wallmaker:addEventListener( “touch”, wallMaker )
function gravity( event )
local phase = event.phase
if “ended” == phase then
physics.setGravity ( 10, 0 )
end
end
launch:addEventListener( “touch”, gravity )
function gravityup( event )
local phase = event.phase
if “ended” == phase then
physics.setGravity ( 0, 0 )
end
end
editButton:addEventListener( “touch”, gravityup )
local function editMode(event)
end
editButton:addEventListener( “touch”, editMode)
local launch = display.newImage( “launch.png”, 200, 20)
local ball = display.newImage( “bird.png” )
ball.x = 625; ball.y = 150
physics.addBody( ball, { density = 15,friction=0.5, bounce=0.3, radius=18} )
ball.linearDamping = 0.3
ball.angularDamping = 0.8
ball.isBullet = true – force continuous collision detection, to stop really fast shots from passing through other balls
target = display.newImage( “bird.png” )
target.x = ball.x; target.y = ball.y; target.alpha = 0
local function resetball(event)
local phase = event.phase
if “ended” == phase then
ball:setLinearVelocity( 0, 0 )
ball.angularVelocity = 0
ball.alpha = 0
ball.x = 625
ball.y = 150
ball.xScale = 2.0; ball.yScale = 2.0
ball.bodyType = “static”
local dropBall = transition.to( ball, { alpha=1.0, xScale=1.0, yScale=1.0, time=400 } )
end
end
Reset:addEventListener( “touch”, resetball )
cannon1 = display.newImage( “cannon.png”,600,70 )
cannon1.targetRotation = 0
cannon1.isRotating = false
cannon1.isTouched = false
local checkKeepRot
function rotateObj(obj)
if obj.rotation >= 360 then
obj.rotation = obj.rotation - 360
end
obj.targetRotation = obj.targetRotation + 10
if obj.targetRotation > 90 then
obj.targetRotation = obj.targetRotation - 100
end
–print(“targetRot=”…obj.targetRotation)
transition.to(obj, { time=150, rotation = obj.targetRotation, onComplete=checkKeepRot })
obj.isRotating = true
end
function checkKeepRot(target)
if target.isTouched == true then
rotateObj(target)
else
target.isRotating = false
end
end
function move(event)
if event.phase==“began” then
if cannon1.isRotating == false then
rotateObj(cannon1)
end
cannon1.isTouched = true
end
if event.phase==“ended” then
cannon1.isTouched = false
end
end
local checkKeepRot
function rotateObj(obj)
if obj.rotation >= 360 then
obj.rotation = obj.rotation - 360
end
obj.targetRotation = obj.targetRotation + 10
if obj.targetRotation > 90 then
obj.targetRotation = obj.targetRotation - 100
end
–print(“targetRot=”…obj.targetRotation)
transition.to(obj, { time=150, rotation = obj.targetRotation, onComplete=checkKeepRot })
obj.isRotating = true
end
function checkKeepRot(target)
if target.isTouched == true then
rotateObj(target)
else
target.isRotating = false
end
end
function move(event)
if event.phase==“began” then
if cannon1.isRotating == false then
rotateObj(cannon1)
end
cannon1.isTouched = true
end
if event.phase==“ended” then
cannon1.isTouched = false
end
end
function shoot(event)
if event.phase==“began” then
ball:setLinearVelocity( 0, 0 )
ball.angularVelocity = 0
ball.alpha = 0
ball.x = 600
ball.y = 150
ball.xScale = .8; ball.yScale = .8
local dropBall = transition.to( ball, { alpha=1.0, xScale=1.0, yScale=1.0, time=40 } )
physics.setGravity( 10, 0 )
ball.bodyType = “dynamic”
ball:applyForce( -000, 30000, ball.x, ball.y )
boom = display.newImage(“boom.png”)
boom.x = 625
boom.y = 150
transition.to( boom, { time=1500, alpha=0, xScale=1.5, yScale=1.5 } )
print( "postCollision force: " … event.force … ", friction: " … event.friction )
end
end
launch:addEventListener(“touch”, shoot)
cannon1:addEventListener(“touch”, move)
[/lua] [import]uid: 28068 topic_id: 6457 reply_id: 22436[/import]