Ball touch

I want to go into the restart screen when I touch the ball in my app but i don’t know if I need an id can anyone help me ? 

local physics = require "physics" local storyboard = require( "storyboard" ) local scene = storyboard.newScene() function SetDefaultAnchor( pos ) display.setDefault("anchorX", pos.x) display.setDefault("anchorY", pos.y) end function CreateWalls(BorderWidth) -- make the math easier local OldAnchor = {x = display.getDefault(anchorX), y = display.getDefault(anchorY) } SetDefaultAnchor({x=0, y=0}) local Height = display.contentHeight -- + (2 \* BorderWidth) local Width = display.contentWidth -- + (2 \* BorderWidth) local leftWall = display.newRect(0, 0, BorderWidth, Height) local rightWall = display.newRect(Width - BorderWidth, 0, BorderWidth, Height) local ceiling = display.newRect(0, 0, Width, BorderWidth) local floor = display.newRect(0, Height-BorderWidth, Width, BorderWidth) physics.addBody (leftWall, "static", {bounce = 0.1, friction = 2}) physics.addBody (rightWall, "static", {bounce = 0.1, friction = 2}) physics.addBody (ceiling, "static", {bounce = 0.1, friction = 2}) physics.addBody (floor, "static", {bounce = 0.1, friction = 2}) -- restore previous defaults SetDefaultAnchor(OldAnchor) end physics.start() -- let's make stuff yellow display.setDefault("fillColor", 0, 1, 0) CreateWalls(1) -- create image of a ball Ball = display.newCircle(100, 100, 10) -- define physics object attached to image of ball physics.addBody(Ball, "dynamic", {friction=3}) -- give ball a shove Ball:applyLinearImpulse(-.09, .08, 0, 0) function onBallTouch( event ) if event.phase == "began" then storyboard.gotoScene("restart", "fade", 500) end end return scene

That doesn’t look like a complete scene. I also don’t see where you’re adding your onBallTouch to your ball, or where you’re creating it.

Rob

-- requires local physics = require "physics" physics.start() local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- background function scene:createScene(event) local screenGroup = self.view function SetDefaultAnchor( pos ) display.setDefault("anchorX", pos.x) display.setDefault("anchorY", pos.y) end function CreateWalls(BorderWidth) -- make the math easier local OldAnchor = {x = display.getDefault(anchorX), y = display.getDefault(anchorY) } SetDefaultAnchor({x=0, y=0}) local Height = display.contentHeight -- + (2 \* BorderWidth) local Width = display.contentWidth -- + (2 \* BorderWidth) local leftWall = display.newRect(0, 0, BorderWidth, Height) local rightWall = display.newRect(Width - BorderWidth, 0, BorderWidth, Height) local ceiling = display.newRect(0, 0, Width, BorderWidth) local floor = display.newRect(0, Height-BorderWidth, Width, BorderWidth) physics.addBody (leftWall, "static", {bounce = 0.7, friction = 2}) physics.addBody (rightWall, "static", {bounce = 0.0, friction = 2}) physics.addBody (ceiling, "static", {bounce = 0.8, friction = 2}) physics.addBody (floor, "static", {bounce = 0.0, friction = 2}) -- restore previous defaults SetDefaultAnchor(OldAnchor) end physics.start() -- let's make stuff yellow display.setDefault("fillColor", 0, 1, 1) CreateWalls(1) -- create image of a ball Ball = display.newCircle(100, 100, 10) -- define physics object attached to image of ball physics.addBody(Ball, "dynamic", {friction=2}) -- give ball a shove Ball:applyLinearImpulse(-.05, .05, 0, 0) end function onBallTap( event ) if event.phase =="began" then storyboard.gotoScene("restart", "fade", 400) end end function scene:enterScene(event) Runtime:removeEventListener("tap", onBallTap ) end function scene:exitScene(event) Runtime:removeEventListener("tap", onBallTap) end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

When the user taps the ball I want to go to the restart screen 

When the user taps the ball I want to go to the restart screen 

You have a couple of issues going on. First this block of code:

physics.start() -- let's make stuff yellow display.setDefault("fillColor", 0, 1, 1) CreateWalls(1) -- create image of a ball Ball = display.newCircle(100, 100, 10) -- define physics object attached to image of ball physics.addBody(Ball, "dynamic", {friction=2}) -- give ball a shove Ball:applyLinearImpulse(-.05, .05, 0, 0)

Is not part of any function. It’s setting in the scene’s main chunk. That means it will run once, when the scene is first required. You would have to specifically remove the scene completely for this to re-run.  This type of code should be inside of your scene:createScene() function, or inside a function that gets called from scene:createScene().

Secondly and more importantly, you are never adding these display objects into the scene’s view group. If you created these inside of scene:createScene() then after you created an object, you could add it to the scene group.

function scene:createScene(event)     local screenGroup = self.view         Ball = display.newCircle(100, 100, 10)     screenGroup:insert( Ball ) end

and you never add a tap listener to the ball. You try to remove a tap listener in scene:enterScene() but you never add it. I would personally add the listener when you create the ball and you don’t need to remove it since Storyboard will handle it for you.

function scene:createScene(event) local screenGroup = self.view Ball = display.newCircle(100, 100, 10) screenGroup:insert( Ball ) Ball:addEventListener( "tap", onBallTap ) end

But this leads to the next problem you have to solve. You have programmed onBallTap to be a touch handler, not a tap handler. Touch events have phases like “began” and “ended”. Tap events don’t have phases. If you want to use “tap” then that function should look like:

local function onBallTap( event ) storyboard.gotoScene("restart", "fade", 400) return true end

Now I made the function local. You have it in your code after scene:createScene() where we need to reference the function. You should move this function above the function scene:createScene() function in your code. Why did I make it local? Well you are using a lot of global variables (such as Ball). Globals are bad and you are probably using them because you don’t understand “scope”. You should read this tutorial: https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/ perhaps multiple times until you get “scope”.

You will run into other issues as you continue with your code, but lets get this working first and then we can come back and look at fixing the rest of the things.

Rob

That doesn’t look like a complete scene. I also don’t see where you’re adding your onBallTouch to your ball, or where you’re creating it.

Rob

-- requires local physics = require "physics" physics.start() local storyboard = require( "storyboard" ) local scene = storyboard.newScene() -- background function scene:createScene(event) local screenGroup = self.view function SetDefaultAnchor( pos ) display.setDefault("anchorX", pos.x) display.setDefault("anchorY", pos.y) end function CreateWalls(BorderWidth) -- make the math easier local OldAnchor = {x = display.getDefault(anchorX), y = display.getDefault(anchorY) } SetDefaultAnchor({x=0, y=0}) local Height = display.contentHeight -- + (2 \* BorderWidth) local Width = display.contentWidth -- + (2 \* BorderWidth) local leftWall = display.newRect(0, 0, BorderWidth, Height) local rightWall = display.newRect(Width - BorderWidth, 0, BorderWidth, Height) local ceiling = display.newRect(0, 0, Width, BorderWidth) local floor = display.newRect(0, Height-BorderWidth, Width, BorderWidth) physics.addBody (leftWall, "static", {bounce = 0.7, friction = 2}) physics.addBody (rightWall, "static", {bounce = 0.0, friction = 2}) physics.addBody (ceiling, "static", {bounce = 0.8, friction = 2}) physics.addBody (floor, "static", {bounce = 0.0, friction = 2}) -- restore previous defaults SetDefaultAnchor(OldAnchor) end physics.start() -- let's make stuff yellow display.setDefault("fillColor", 0, 1, 1) CreateWalls(1) -- create image of a ball Ball = display.newCircle(100, 100, 10) -- define physics object attached to image of ball physics.addBody(Ball, "dynamic", {friction=2}) -- give ball a shove Ball:applyLinearImpulse(-.05, .05, 0, 0) end function onBallTap( event ) if event.phase =="began" then storyboard.gotoScene("restart", "fade", 400) end end function scene:enterScene(event) Runtime:removeEventListener("tap", onBallTap ) end function scene:exitScene(event) Runtime:removeEventListener("tap", onBallTap) end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

When the user taps the ball I want to go to the restart screen 

When the user taps the ball I want to go to the restart screen 

You have a couple of issues going on. First this block of code:

physics.start() -- let's make stuff yellow display.setDefault("fillColor", 0, 1, 1) CreateWalls(1) -- create image of a ball Ball = display.newCircle(100, 100, 10) -- define physics object attached to image of ball physics.addBody(Ball, "dynamic", {friction=2}) -- give ball a shove Ball:applyLinearImpulse(-.05, .05, 0, 0)

Is not part of any function. It’s setting in the scene’s main chunk. That means it will run once, when the scene is first required. You would have to specifically remove the scene completely for this to re-run.  This type of code should be inside of your scene:createScene() function, or inside a function that gets called from scene:createScene().

Secondly and more importantly, you are never adding these display objects into the scene’s view group. If you created these inside of scene:createScene() then after you created an object, you could add it to the scene group.

function scene:createScene(event)     local screenGroup = self.view         Ball = display.newCircle(100, 100, 10)     screenGroup:insert( Ball ) end

and you never add a tap listener to the ball. You try to remove a tap listener in scene:enterScene() but you never add it. I would personally add the listener when you create the ball and you don’t need to remove it since Storyboard will handle it for you.

function scene:createScene(event) local screenGroup = self.view Ball = display.newCircle(100, 100, 10) screenGroup:insert( Ball ) Ball:addEventListener( "tap", onBallTap ) end

But this leads to the next problem you have to solve. You have programmed onBallTap to be a touch handler, not a tap handler. Touch events have phases like “began” and “ended”. Tap events don’t have phases. If you want to use “tap” then that function should look like:

local function onBallTap( event ) storyboard.gotoScene("restart", "fade", 400) return true end

Now I made the function local. You have it in your code after scene:createScene() where we need to reference the function. You should move this function above the function scene:createScene() function in your code. Why did I make it local? Well you are using a lot of global variables (such as Ball). Globals are bad and you are probably using them because you don’t understand “scope”. You should read this tutorial: https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/ perhaps multiple times until you get “scope”.

You will run into other issues as you continue with your code, but lets get this working first and then we can come back and look at fixing the rest of the things.

Rob