Hi All,
I’ve been loving Corona SDK for it’s ease of use of lua and the features Corona hands out of the box.
I’ve been blocked for a while on this scene where the Projectile (ball) after it releasing an elastic band does not collide with static objects.
This is the Ball Class:
[lua]module(…, package.seeall)
– Pass state reference
state = {};
– Bullet starts off in-active
ready = false;
– Pass audio references
shot = {};
band_stretch = {};
function newProjectile()
– Import easing plugin
local easingx = require(“easing”);
– Bullet properties
local bun_bullet = {
name = “pepsiBall”, – bun
type = “bullet”,
density=0.8,
friction=0.3,
bounce=0.6,
size = 49,
rotation = 30
}
– Init bullet
local bullet = display.newImage(“images/ammo/” … bun_bullet.name … “.png”);
– Place bullet
bullet.x = 170; bullet.y = _H + 20;
– Set up physical properties
physics.addBody(bullet, “kinematic”, {density=bun_bullet.density, friction=bun_bullet.friction, bounce=bun_bullet.bounce, radius=bun_bullet.size});
bullet.linearDamping = 0.3;
bullet.angularDamping = 0.8;
bullet.isBullet = true;
bullet.isSensor = true;
– Transition the bullet into position each time it’s spawned
transition.to(bullet, {time=600, y=_H - 140, transition = easingx.easeOutElastic});
return bullet;
end[/lua]
and this is the main scene
[lua]local storyboard = require( “storyboard” )
local scene = storyboard.newScene()
local ui = require “scripts.lib.ui”
local radlib = require “scripts.lib.radlib”
local physics = require “physics”
physics.start()
physics.setGravity(9.81) – 9.81 m/s*s in the positive y direction
physics.setScale(80) – 80 pixels per meter
physics.setDrawMode(“normal”)
local _WCenter = display.contentWidth/2;
local _HCenter = display.contentHeight/2;
local _W = display.contentWidth;
local _H = display.contentHeight + 20;
– BEGINNING OF YOUR IMPLEMENTATION
function scene:createScene( event )
local screenGroup = self.view
– Create the bounds of the “arena”
local background = display.newImage( “assets/playground.png” )
background.x = _WCenter;
background.y = _HCenter;
– Screen borders
local floor = display.newRect(_W, 0, 1, _H)
local lWall = display.newRect(0, _H, _W, 1)
local rWall = display.newRect(0, -1, _W, 1)
local ceiling = display.newRect(-1, 0, 1, _H)
screenGroup:insert(background)
screenGroup:insert(floor)
screenGroup:insert(lWall)
screenGroup:insert(rWall)
screenGroup:insert(ceiling)
staticMaterial = {density=2, friction=.3, bounce=.4}
physics.addBody(floor, “static”, staticMaterial)
physics.addBody(lWall, “static”, staticMaterial)
physics.addBody(rWall, “static”, staticMaterial)
physics.addBody(ceiling, “static”, staticMaterial)
–BALL
local ball = display.newImage( “assets/pepsiBall.png”)
ball.y = 150
physics.addBody(ball, {density=.8, friction=.3, bounce=.6, radius=49})
screenGroup:insert(ball)
– SCORE
local score = display.newText(“Score: 0”, _WCenter + 60, 5)
score:setTextColor(255, 255, 255, 255)
score.size = 25
screenGroup:insert(score)
– End step 2
local speedX = 0
local speedY = 0
local prevTime = 0
local prevX = 0
local prevY = 0
– A basic function for dragging physics objects
local function drag( event )
local ball = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( ball )
– Store initial position
ball.x0 = event.x - ball.x
ball.y0 = event.y - ball.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
else
if “moved” == phase then
ball.x = event.x - ball.x0
ball.y = event.y - ball.y0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
event.target.bodyType = “dynamic”
ball:setLinearVelocity(speedX, speedY)
end
end
– Stop further propagation of touch event!
return true
end
function trackVelocity(event)
local timePassed = event.time - prevTime
prevTime = prevTime + timePassed
speedX = (ball.x - prevX)/(timePassed/1000)
speedY = (ball.y - prevY)/(timePassed/1000)
prevX = ball.x
prevY = ball.y
end
Runtime:addEventListener(“enterFrame”, trackVelocity)
ball:addEventListener(“touch”, drag)
–Create the hoop and scoring mechanism
local rimNet = display.newRect(_WCenter - 100, 40, 190, 9)
rimNet:setFillColor(255, 255, 255)
rimNet.alpha = 0.8
screenGroup:insert(rimNet)
local leftBorder = display.newRect(_WCenter - 100,30, 9, 25)
leftBorder:setFillColor(89,58,35)
screenGroup:insert(leftBorder)
local rightBorder = display.newRect(_WCenter + 85,30, 9, 25)
rightBorder:setFillColor(89,58,35)
screenGroup:insert(rightBorder)
physics.addBody(leftBorder, “static”, staticMaterial)
physics.addBody(rightBorder, “static”, staticMaterial)
scoreCtr = 0
local lastGoalTime = 1000
function monitorScore(event)
if event.time - lastGoalTime > 500 then
if ball.x > _WCenter - 100 and ball.x < _WCenter + 85 and ball.y < 65 then
scoreCtr = scoreCtr + 1
print(score)
lastGoalTime = event.time
score.text = "Score: " … scoreCtr
if scoreCtr == 1 then
local infoMessage = display.newText(“Mabrouk, one more”, _WCenter, _HCenter)
infoMessage:setTextColor(255, 255, 255, 255)
infoMessage.size = 25
transition.to( infoMessage, { time=2000, alpha=0, x=(_WCenter-50), y=(_HCenter-50) } )
screenGroup:insert(infoMessage)
elseif scoreCtr == 2 then
Runtime:removeEventListener(“enterFrame”, trackVelocity)
Runtime:removeEventListener(“enterFrame”, monitorScore)
storyboard.gotoScene( “play2” )
end
end
end
end
Runtime:addEventListener(“enterFrame”, monitorScore)
end
function scene:enterScene( event )
print(“Play loaded…”)
storyboard.removeAll()
end
function scene:exitScene( event )
end
function scene:destroyScene( event )
end
– END OF YOUR IMPLEMENTATION
–
– “createScene” event is dispatched if scene’s view does not exist
scene:addEventListener( “createScene”, scene )
– “enterScene” event is dispatched whenever scene transition has finished
scene:addEventListener( “enterScene”, scene )
– “exitScene” event is dispatched before next scene’s transition begins
scene:addEventListener( “exitScene”, scene )
– “destroyScene” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroyScene”, scene )
return scene[/lua]
If you run this code, you’ll notice once you e.phase ends, I change t.bodyType = “dynamic”; and yet it still dosen’t collide. Any thoughts, or game logic I’m missing? [import]uid: 141821 topic_id: 29622 reply_id: 329622[/import]