So if I shoot an object on the left side of the gun, let’s say if the object shows up on the x-axis from 0-200 and another object shows up from 210-570, how can I trigger a transition/animation effect based on which location on the x-axis the object gets hit with a bullet? For example, I shoot a bird on the left side, I trigger an animation showing the bird falling but moving offscreen to the left. The same goes when I shoot the bird on the right side of the screen but it’s moving to the right offscreen.
I can easily do this by creating two different functions and a math.random code for the x axis (0, 200 or 210, 570) but I prefer one object and one function. Any help will be appreciated!
What is the math.random for?
Oh nevermind, I resolved the problem
Here’s something simple might help you
[lua]local _w, _h = display.actualContentWidth, display.actualContentHeight
local bird = display.newRect( _w*math.random(), _h*0.5, 44, 44)
local function touch( event )
local phase = event.phase
local x = event.x
if phase == “began” then
if x < _w*0.5 then
transition.to( bird, { time = 1000, x = 0 } )
else
transition.to( bird, { time = 1000, x = _w } )
end
end
return true
end
bird:addEventListener( “touch”, touch )[/lua]
What is the math.random for?
Oh nevermind, I resolved the problem
Here’s something simple might help you
[lua]local _w, _h = display.actualContentWidth, display.actualContentHeight
local bird = display.newRect( _w*math.random(), _h*0.5, 44, 44)
local function touch( event )
local phase = event.phase
local x = event.x
if phase == “began” then
if x < _w*0.5 then
transition.to( bird, { time = 1000, x = 0 } )
else
transition.to( bird, { time = 1000, x = _w } )
end
end
return true
end
bird:addEventListener( “touch”, touch )[/lua]