Hello, I’ve been having a problem using the physics engine where I have a moving object (dynamic, transition) and a draggable object (kinematic, responds to touch).
When the collision occurs, I want it to cause the draggable object to fade(I managed this with transition.dissolve), remove the dynamic object from view and display an image in it’s place.
I have uploaded a video to show the problem:
[media]http://www.youtube.com/watch?v=pAldp55q4jo&feature=youtu.be[/media]
Basically, the error is where the purple(kinematic) object fades without appearing to have collided with the transitioning (orange) object.
It correctly fades when the orange and purple collide also.
Code:
[lua]system.setIdleTimer(false)
– allow user’s background music to play
–audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode)
– Hide Status Bar
display.setStatusBar(display.HiddenStatusBar)
– FORWARD REFERENCES --Call functions locally but with a global scope
local centerX = display.contentCenterX
local centerY = display.contentCenterY
–Variables
local score = 0
local timer = 0
local dodgeCount = 0d
local bg
local bg2
local spaceship
local enemy
enemyHit = false
–Functions
local easyGame
local easyStart
–Filters
–local enemyCollisionFilter = { categoryBits = 1, maskBits = 1}
–DEFINE LOCAL VARIABLES {AND TABLES}
–local sounds
–local sounds={}
– LIBRARIES – load global libraries that will be used throughout the app
physics = require(‘physics’)
physics.setDrawMode(‘hybrid’)
physics.start()
physics.setGravity(0,0)
– GRAPHICS
– Load Graphics
– AUDIO
local hit = audio.loadSound(‘hit.wav’)
– Main Functions
–Menu Interface
–(Note: X=Left/Right, Y=Up/Down | - = Left, + = Right , | - = Up, + = Down) (>> = + | << = - | V = + | ^ = -)
function onLocalCollision( event )
enemyHit=true
transition.dissolve(spaceship, spaceship, 5000, 0)
end
easyStart = function()
enemy = display.newImageRect(‘images/spaceship.png’, 50, 100)
enemy.x = 400
enemy.y = math.random(20, display.contentHeight-20)
enemy.trans = transition.to (enemy, { x=-40, time=math.random(1600, 2000), delay=1000, onComplete=easyStart})
physics.addBody(enemy, ‘dynamic’)
enemy.collision = onLocalCollision
enemy:addEventListener(‘collision’, enemy)
end
Runtime:addEventListener(‘collision’, onLocalCollision)
function easyScoring()
timer = timer + 1
scoreTxt.text = ‘Score: ‘…score…’ Time:’… timer
scoreTxt.x = centerX
scoreTxt.y = 450
score = score + 2
if (enemyHit == true) then
enemyHit = false
Hit = display.newImage(‘images/hit.png’, enemy.x, enemy.y)
–display.remove(Hit)
display.remove(enemy)
end
end
Runtime:addEventListener(‘enterFrame’, easyScoring)
–Easy Mode
function easyGame()
bg = display.newImage(‘images/gameBg.png’)
bg:setReferencePoint(display.BottomLeftReferencePoint)
bg.alpha = 0
bg.speed = 4
bg.x = -300
bg2 = display.newImage(‘images/gameBg.png’)
bg2:setReferencePoint(display.BottomLeftReferencePoint)
bg2.x = 0
bg2.speed = 4
bg2.alpha = 0
spaceship = display.newImageRect( “images/spaceship.png”, 50, 100 )
spaceship.x = display.contentCenterX
spaceship.y = display.contentCenterY
spaceship.alpha = 0
spaceship.xScale= 2
spaceship.yScale= 2
physics.addBody(spaceship, ‘kinematic’)
scoreTxt = display.newText(('score: ’ … score), 0, 0, “Helvetica”, 15)
----dodge = display.newText(('Dodge Count: ’ … dodgeCount), 0, 0, “Helvetica”, 15)
–transition effect
transition.to( bg, { time=2000, alpha=1} )
transition.to( bg2, { time=1000, alpha=1} )
transition.to( spaceship, { time=2000, alpha=1, xScale=1, yScale=1, x=200, onComplete=easyStart } )
–interface
spaceship.collision = onLocalCollision
spaceship:addEventListener(‘collision’, onLocalCollision)
end
–change this after interface is done.
–Call easyGame --This must be written after the function or it will cause errors. --Write before spaceship:touch or errors will occur.
easyGame()
function scrollbg(self, event)
–self.x = self.x + 4
if self.x > 300 then
self.x = -300
else
self.x = self.x + self.speed
end
end
bg.enterFrame = scrollbg
Runtime:addEventListener(‘enterFrame’, bg)
bg2.enterFrame = scrollbg
Runtime:addEventListener(‘enterFrame’, bg2)[/lua]
I have reviewed and twiddled with my code about 20+ times but can’t seem to sort it.
Any help is much appreciated.