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 :)

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.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

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.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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

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

Why not just do :

1
2
3
if paused == true then
    physics.pause()
end

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.

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?

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?

views:2015 update:2011/10/18 15:01:22
corona forums © 2003-2011