Start/Restart Button

Hi everyone,

In the game I’m attempting to create I’m hoping for a ‘pause mode’ and a ‘play mode’. In pause mode you can drag and position items across the screen then when hitting play, physics turns on and the actions will take place.

Right now all I have are two buttons which pause and start physics. This doesn’t work because objects are not draggable while physics is paused. Also, there should only be one button to avoid cheating. This would need to toggle from “play” to “restart” when clicked.

The most fantastic solution I could hope for would give me a way to incorporate a ‘toggle button’ and allow dragging of specific objects while physics is paused.

Thanks for your time, I super appreciate any help you can give :slight_smile: [import]uid: 90223 topic_id: 16105 reply_id: 316105[/import]

I suppose you could just hide all of the physics objects, and show placeholder images when they pause. That way, they aren’t really moving the physics objects.

The user would then drag these non-physics placeholders around the screen.

Then, when they resume physics - just hide the placeholder images, and show all the physics objects again, using the positions of the placeholders. [import]uid: 49447 topic_id: 16105 reply_id: 59879[/import]

Sounds great, though I’m not sure I know how to do it - how’s this for a first attempt?

[code]function localDrag( event )
gameUI.dragBody( event, {maxForce=20000, frequency=10, dampingRatio=0.2, center=true} )
end

local placeholderball = display.newImage(“graphics/ball.png”)
placeholderball.x = 160
placeholderball.y = 200
placeholderball:addEventListener( “touch”, dragBody )

local function placeholder( event ) --creating the function for displaying placeholders
if physics.pause() == true then
ball.isVisible = false – hide object
placeholderball – insert variable?
end
end

local function play( event )
if physics.pause == false then
ball.x = placeholderball.x
ball.y = placeholderball.y
physics.addBody(ball, {density=0.9, friction=0.3, bounce=0.2})
end
end

local ball = display.newImage(“graphics/ball.png”)
physics.addBody(ball, {density=0.9, friction=0.3, bounce=0.2})
pausebutton:addEventListener( “touch”, placeholder ) --listening for the button[/code] [import]uid: 90223 topic_id: 16105 reply_id: 59885[/import]

I can’t run it to test since it’s not the complete code, but on line 22, I don’t think you actually need to (or should) re-add the object to physics.

Just set the isVisible back to true, and set placeholderball.isVisible = false

Otherwise, that basically represents the concept I was thinking of. [import]uid: 49447 topic_id: 16105 reply_id: 59892[/import]

Hey again, unfortunately after testing it I can’t get it to work as it should. The placeholder is not draggable and the function set up just seems to pause the physics rather than doing what was intended.

Here’s a full copy of the code if you don’t mind taking a look over:

[code]module(…, package.seeall)

function new()
local localGroup = display.newGroup()

require “sprite”
require “toggle”
local ui = require(“ui”)
local gameUI = require(“gameUI”)
local physics = require(“physics”)
physics.start()
physics.setGravity(0, 9.8)
physics.pause()

local dragBody = gameUI.dragBody – for use in touch event listener below
system.activate(“multitouch”)
display.setStatusBar(display.HiddenStatusBar)
physics.setDrawMode(“hybrid”) – overlays collision outlines on normal Corona objects


– Functions –

local function placeholder( event ) --creates the function for displaying placeholders
if physics.pause() == true then
transparentball.isVisible = false – hide objects
placeholderball.isVisible = true – insert variable?
end
end

local function play( event )
if physics.pause == false then
transparentball.x = placeholderball.x
transparentball.y = placeholderball.y
transparentball.isVisible = true
placeholderball.isVisible = false
end
end

function physicspause (event)
physics.pause()
end

function physicsstart (event)
physics.start()
end

function localDrag( event )
gameUI.dragBody( event, {maxForce=20000, frequency=10, dampingRatio=0.2, center=true} )
end


– PLACEHOLDERS –

local placeholderball = display.newImage(“graphics/transparentball.png”)
placeholderball.x = 160
placeholderball.y = 200
placeholderball:addEventListener( “touch”, dragBody )

–DRAW IMAGES

local background1 = display.newImage( “graphics/background.png” )
background1.x = 160
background1.y = 240
background1.xScale = 2.0
background1.yScale = 2.0

local btn1 = display.newImage(“graphics/button.png”)
btn1.x = 150
btn1.y = 10
btn1.xScale = 0.300
btn1.yScale = 0.300
btn1:addEventListener(“touch”, placeholder)

local btn2 = display.newImage(“graphics/button.png”)
btn2.x = 300
btn2.y = 10
btn2.xScale = 0.300
btn2.yScale = 0.300
btn2:addEventListener(“touch”, physicsstart)

local instruction = display.newText( “drag any object”, 80, 60, native.systemFontBold, 20 )
instruction:setTextColor( 255, 255, 255, 500 )

local ground = display.newImage(“graphics/floor.png”) – physical ground object
ground.x = 165
ground.y = display.contentHeight - ground.contentHeight/2
physics.addBody( ground, “static”, { friction=0.5, bounce=0.3 } )

local transparentball = display.newImage(“graphics/transparentball.png”)
transparentball.x = placeholderball.x
transparentball.y = placeholderball.y
transparentball.xScale = 1
transparentball.yScale = 1
physics.addBody(transparentball, {density=0.9, friction=0.3, bounce=0.2})
transparentball:addEventListener( “touch”, dragBody )

local yellow = display.newImage(“graphics/yellowball.png”)
yellow.x = 300
yellow.y = 200
physics.addBody(yellow, {density=0.9, friction=0.3, bounce=0.3})

local orange = display.newImage(“graphics/orangeball.png”)
orange.x = 400
orange.y = 200
orange.xScale = 0.1
orange.yScale = 0.1
physics.addBody(orange, {density=0.9, friction=0.3, bounce=0.3})
if transparentball.currentFrame == 3 then
– do something (win condition)
end

transparentball.myName = “transparentball”
red.myName = “red”

localGroup:insert(background1)
localGroup:insert(instruction)
localGroup:insert(ground)
localGroup:insert(transparentball)
localGroup:insert(placeholderball)
localGroup:insert(orange)
localGroup:insert(yellow)
localGroup:insert(btn1)
localGroup:insert(btn2)


return localGroup
end[/code] [import]uid: 90223 topic_id: 16105 reply_id: 59902[/import]

Why go to all that bother? You want the physics objects to not move during a pause right ?

Why not just do :

  
if paused == true then  
 physics.pause()  
end  
  

Edit : Why aren’t the objects draggable whilst physics is paused? [import]uid: 84637 topic_id: 16105 reply_id: 59909[/import]

Just wanted to point out, I can’t think of a reason why objects wouldn’t be draggable while physics is paused, like Danny is asking.

My suggestion was based on the assumption that they can’t be dragged. If they can, (they probably should be able to) then this is indeed the long route. [import]uid: 49447 topic_id: 16105 reply_id: 59913[/import]

Why go to all that bother? You want the physics objects to not move during a pause right ?

Actually I want the opposite, I want the physics objects to move (be draggable) during a pause.

Edit : Why aren’t the objects draggable whilst physics is paused?

I have honestly no idea! Can you think of any reasons?

Just wanted to point out, I can’t think of a reason why objects wouldn’t be draggable while physics is paused, like Danny is asking. My suggestion was based on the assumption that they can’t be dragged. If they can, (they probably should be able to) then this is indeed the long route.

Neither can I, although as long as there’s a solution I guess it’s not a problem. That being said I still can’t get the placeholders to work since they are not draggable while physics is paused either, any other suggestions? [import]uid: 90223 topic_id: 16105 reply_id: 59936[/import]

As another piece of information, whenever I do try and drag the placeholder the simulator force closes.

Also by scaling the xScale to 500 and the yScale to 0.2 of the placeholder, it can be seen that the real ball (not the placeholder) is still there for some reason behind the placeholder however it is only when I try to drag the placeholder that it force closes.

I thought I made the ball invisible?

Odd, right? [import]uid: 90223 topic_id: 16105 reply_id: 60025[/import]