Hi all, I’m trying to create the simplest concept ever, but I’ve having a total mare.
An old flash game that I’m trying to re-make as a learning experiment, but as I don’t have the luxury of being able to make vectors collision sensitive, it’s a tough gig.
Here’s a rough outline:
It’s like playing Atari Pong for one… Your ‘bats’ (a series of recatangles that diminish as the game progresses) is on a circular orbital, you touch the right side of the screen to orbit the bats clockwise and the left for anti-clockwise, hitting the ball back and forth (to yourself)… The game gets progressively faster. That’s kind’ve it really.
After spending ages with a problem with event.phase == ended, I now seem to be having a set back with creating a simple orbital behaviour for my bat, Graphics 2, here we go again.
As you’ll know, x/yReference has been totally binned, and as far as I can see, never replaced… I’ve had a play around with anchorChildren and even containers, and have gotten quite close, to the right kind of movement at least… The problem is; my bats are obviously physics objects, and for some reason, the physics bodies either don’t track the rotational value of it’s parent-objects, when using anchor children (in this case, just an imageRect) or they just map to the wrong co-ordinates completely (even though in hybrid mode, the body appears over the image ((sort of)) the ‘ball’ bounces off an invisible ‘bat’).
For those who are that way inclined, I’ve attahced the code…
Usually where I fall short is math. I suspect that the answer lies in rads, but I’m just not good enough… Oh, I can’t put the ball in the same group as the bat objects either, especially if I use anchorChildren… The ball’s trajectory obviously follows the group’s rotation value.
Help, I’m properly stuck with this one:
local storyboard = require( "storyboard" ) local widget = require( "widget" ) local scene = storyboard.newScene() local physics = require("physics") physics.setDrawMode( "hybrid" ) ---------------------------------------------------------------------------------- physics.start() physics.setGravity( 0, 0 ) score = 0 --------------------------------------------------------------------------------- local bg local delta local centre local ball local scoreNumber local scoreText local rotateRight local rotateLeft local brick1 local brick2 local brick3 local brick4 local brick5 local brick6 local brick7 local brick8 local brick9 local brick10 --local brick11 --local brick12 ------local brick13 ----local brick14 --local brick15 --local brick16 --local brick17 --local brick18 --local brick19 --local brick20 --local brick21 --local brick22 --local brick23 --local brick24 --local brick25 --local brick26 --local brick27 --local brick28 --local brick29 --local brick30 --local brick31 --local brick32 --local brick33 --local brick34 --local brick35 --local brick36 local function level2() -- end --local function increaseDifficulty() --if score =\>4 and \<10 then -- level2 () --else --do nothing --end --end local function updateScore() score = score + 1 scoreNumber.text = score end local function onLocalCollision( self, event ) if ( event.phase == "began" ) then updateScore() elseif ( event.phase == "ended" ) then -- increaseDifficulty() end end --// THIS JUST ROTATES THE BRICK AROUND IT'S CENTRAL ANCHOR local function onEnterFrame1(event) brick1.rotation = brick1.rotation - 2 end local function turnRight( event ) if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) Runtime:addEventListener("enterFrame", onEnterFrame1) elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus( nil ) Runtime:removeEventListener("enterFrame", onEnterFrame1) end return true end --local function turnLeft(event) --if ( event.phase == "began" ) then -- elseif ( event.phase == "ended" ) then -- end -- return true --end --------------------------------------------------------------------------------- -- BEGINNING OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- Called when the scene's view does not exist: function scene:createScene( event ) local group = self.view bg = display.newImageRect( "assets/bg.png", 640, 1136 ) bg.x = display.contentCenterX bg.y = display.contentCenterY delta = display.newImageRect( "assets/delta-check.png", 570, 570 ) delta.x = display.contentCenterX delta.y = display.contentCenterY centre = display.newCircle( 320, 572, 18 ) centre:setFillColor( 0.5 ) ball = display.newImageRect( "assets/ball.png", 36, 36 ) ball.x = display.contentCenterX ball.y = display.contentCenterY + 2 physics.addBody( ball, "dynamic", { density=3.0, friction=0.0, bounce=1.0, radius=18 } ) ball.isVisible = true ball:applyLinearImpulse( 20, -20, ball.x, ball.y ) ball.collision = onLocalCollision ball:addEventListener( "collision", ball ) scoreText = display.newText("score:", 300, 80, nil, 50) scoreText.xScale = 1.2 scoreText.yScale = 1.2 scoreNumber = display.newText(score, 424, 82, nil, 50) scoreNumber.xScale = 1.2 scoreNumber.yScale = 1.2 rotateRight = display.newRect( 480, 500, 320, 1030 ) rotateRight.strokeWidth = 0 rotateRight:setFillColor( 0.5 ) rotateRight.alpha = .5 rotateRight:addEventListener( "touch", turnRight ) rotateLeft = display.newRect( 160, 500, 320, 1030 ) rotateLeft.strokeWidth = 0 rotateLeft:setFillColor( 0.2 ) rotateLeft.alpha = .5 rotateLeft:addEventListener( "touch", turnLeft ) brick1 = display.newImageRect( "assets/brick.png", 30, 10 ) brick1.x = 320 brick1.y = 333 brick1.rotation = 0 physics.addBody( brick1, "static", { density=3.0, friction=0.5, bounce=0.3 } ) brick1.isVisible = true brick2 = display.newImageRect( "assets/brick.png", 30, 10 ) brick2.x = 360 brick2.y = 337 brick2.rotation = 10 physics.addBody( brick2, "static", { density=3.0, friction=0.5, bounce=0.3 } ) brick2.isVisible = true brick3 = display.newImageRect( "assets/brick.png", 30, 10 ) brick3.x = 400 brick3.y = 348 brick3.rotation = 20 physics.addBody( brick3, "static", { density=3.0, friction=0.5, bounce=0.3 } ) brick3.isVisible = true brick4 = display.newImageRect( "assets/brick.png", 30, 10 ) brick4.x = 437 brick4.y = 365 brick4.rotation = 30 physics.addBody( brick4, "static", { density=3.0, friction=0.5, bounce=0.3 } ) brick4.isVisible = true brick5 = display.newImageRect( "assets/brick.png", 30, 10 ) brick5.x = 469 brick5.y = 389 brick5.rotation = 40 physics.addBody( brick5, "static", { density=3.0, friction=0.5, bounce=0.3 } ) brick5.isVisible = true brick6 = display.newImageRect( "assets/brick.png", 30, 10 ) brick6.x = 498 brick6.y = 417 brick6.rotation = 50 physics.addBody( brick6, "static", { density=3.0, friction=0.5, bounce=0.3 } ) brick6.isVisible = true brick7 = display.newImageRect( "assets/brick.png", 30, 10 ) brick7.x = 522 brick7.y = 452 brick7.rotation = 60 physics.addBody( brick7, "static", { density=3.0, friction=0.5, bounce=0.3 } ) brick7.isVisible = true brick8 = display.newImageRect( "assets/brick.png", 30, 10 ) brick8.x = 539 brick8.y = 488 brick8.rotation = 70 physics.addBody( brick8, "static", { density=3.0, friction=0.5, bounce=0.3 } ) brick8.isVisible = true brick9 = display.newImageRect( "assets/brick.png", 30, 10 ) brick9.x = 549 brick9.y = 528 brick9.rotation = 80 physics.addBody( brick9, "static", { density=3.0, friction=0.5, bounce=0.3 } ) brick8.isVisible = true brick10 = display.newImageRect( "assets/brick.png", 30, 10 ) brick10.x = 552 brick10.y = 569 brick10.rotation = 90 physics.addBody( brick10, "static", { density=3.0, friction=0.5, bounce=0.3 } ) brick10.isVisible = true end -- Called BEFORE scene has moved onscreen: function scene:willEnterScene( event ) local group = self.view local group = display.newGroup() group:insert( bg ) group:insert( delta ) group:insert( rotateRight ) group:insert( rotateLeft ) group:insert( scoreNumber ) group:insert( scoreText ) group:insert( centre) local ballgroup = display.newGroup() ballgroup:insert( ball ) local brickgroup = display.newGroup() brickgroup:insert( brick1 ) brickgroup:insert( brick2 ) brickgroup:insert( brick3 ) brickgroup:insert( brick4 ) brickgroup:insert( brick5 ) brickgroup:insert( brick6 ) brickgroup:insert( brick7 ) brickgroup:insert( brick8 ) brickgroup:insert( brick9 ) brickgroup:insert( brick10 ) --brickgroup:insert( brick11 ) --brickgroup:insert( brick12 ) --brickgroup:insert( brick13 ) --brickgroup:insert( brick14 ) --brickgroup:insert( brick15 ) --brickgroup:insert( brick16 ) --brickgroup:insert( brick17 ) --brickgroup:insert( brick18 ) --brickgroup:insert( brick19 ) --brickgroup:insert( brick20 ) --brickgroup:insert( brick21 ) --brickgroup:insert( brick22 ) --brickgroup:insert( brick23 ) --brickgroup:insert( brick24 ) --brickgroup:insert( brick25 ) --brickgroup:insert( brick26 ) --brickgroup:insert( brick27 ) --brickgroup:insert( brick28 ) --brickgroup:insert( brick29 ) --brickgroup:insert( brick30 ) --brickgroup:insert( brick31 ) --brickgroup:insert( brick32 ) --brickgroup:insert( brick33 ) --brickgroup:insert( brick34 ) --brickgroup:insert( brick35 ) --brickgroup:insert( brick36 ) end -- Called immediately after scene has moved onscreen: function scene:enterScene( event ) local group = self.view ----------------------------------------------------------------------------- -- INSERT code here (e.g. start timers, load audio, start listeners, etc.) ----------------------------------------------------------------------------- end -- Called when scene is about to move offscreen: function scene:exitScene( event ) local group = self.view ----------------------------------------------------------------------------- -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) ----------------------------------------------------------------------------- end -- Called AFTER scene has finished moving offscreen: function scene:didExitScene( event ) local group = self.view ----------------------------------------------------------------------------- -- This event requires build 2012.782 or later. ----------------------------------------------------------------------------- end -- Called prior to the removal of scene's "view" (display group) function scene:destroyScene( event ) local group = self.view ----------------------------------------------------------------------------- -- INSERT code here (e.g. remove listeners, widgets, save state, etc.) ----------------------------------------------------------------------------- end -- Called if/when overlay scene is displayed via storyboard.showOverlay() function scene:overlayBegan( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene ----------------------------------------------------------------------------- -- This event requires build 2012.797 or later. ----------------------------------------------------------------------------- end -- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay() function scene:overlayEnded( event ) local group = self.view local overlay\_name = event.sceneName -- name of the overlay scene ----------------------------------------------------------------------------- -- This event requires build 2012.797 or later. ----------------------------------------------------------------------------- end --------------------------------------------------------------------------------- -- END OF YOUR IMPLEMENTATION --------------------------------------------------------------------------------- -- "createScene" event is dispatched if scene's view does not exist scene:addEventListener( "createScene", scene ) -- "willEnterScene" event is dispatched before scene transition begins scene:addEventListener( "willEnterScene", 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 ) -- "didExitScene" event is dispatched after scene has finished transitioning out scene:addEventListener( "didExitScene", 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 ) -- "overlayBegan" event is dispatched when an overlay scene is shown scene:addEventListener( "overlayBegan", scene ) -- "overlayEnded" event is dispatched when an overlay scene is hidden/removed scene:addEventListener( "overlayEnded", scene ) --------------------------------------------------------------------------------- return scene