Hi,
I have issues solving a problem I’ve encountered, my goal is to have the scene change when a ball enters a ‘goal area’. I use the accelerometer to control the ball, everything is working fine until I reach the point of calling storyboard.gotoScene ( scene, effect, time ). I get no errors from running the code or building it.
I’ll present two of my attempts, the key function lies within the function “scene:enterScene( event )”
-- level1.lua
-----------------------------------------------------------------------------------------
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local startImage, ball
--loads physics and some more locals
--------------------------------------------
local function tapToStart ( self, event )
if event.phase == "began" then
physics.start()
startImage:removeSelf()
return true
end
end
--------------------------------------------
-- CREATE SCENE
function scene:createScene( event )
local group = self.view
local bg = display.newRect(0,-25, vcw, vch+50)
bg:setFillColor(0,255,255)
local b1 = display.newRect(0,-25, 400, 770)
--and some more rectangles
ball = display.newImage("ball.png")
ball.x = 800; ball.y = 60
startImage = display.newImage( "start.png" )
startImage.y = ch\*0.5
b1:setFillColor (255,255,255)
--and some more FillColors
group:insert( bg )
--and some more inserts (all included!)
physics.addBody ( b1, "kinematic", {friction=0.3, bounce=0 } )
--and some more addBody properties
startImage.touch = tapToStart
ball.isSleepingAllowed = false;
end
-- ENTER SCENE
function scene:enterScene( event )
local function onAccelerate( event )
if (ball.x \> 600 and ball.x \< 720 and ball.y \> ch-95) then
physics.stop()
storyboard.gotoScene("level2", "fade", 500) -- physics stop but the scene doesn't change
else
physics.setGravity (0+(event.yGravity\*-15), 10)
end
end
system.setIdleTimer( false )
Runtime:addEventListener("accelerometer", onAccelerate);
startImage:addEventListener("touch", startImage);
end
-- EXIT SCENE
function scene:exitScene( event )
Runtime:removeEventListener("accelerometer", onAccelerate);
startImage:removeEventListener("touch", startImage);
end
-- DESTROY SCENE
function scene:destroyScene( event )
package.loaded[physics] = nil
physics = nil
end
--scene event listeners
--------------------------------------------
return scene
attempt 2:
-- level1.lua
-----------------------------------------------------------------------------------------
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local widget = require "widget"
--physics
local startImage, winImage, ball
--other locals
--------------------------------------------
local function tapToStart ( self, event )
--code
end
local function onBtnRelease()
storyboard.gotoScene( "level2", "fade", 500 )
return true
end
--------------------------------------------
-- CREATE SCENE
function scene:createScene( event )
local group = self.view
local bg = display.newRect(0,-25, vcw, vch+50)
bg:setFillColor(0,255,255)
--rectangles
ball = display.newImage("ball.png")
ball.x = 800; ball.y = 60
startImage = display.newImage( "start.png" )
startImage.y = ch\*0.5
winImage = widget.newButton{
default="win.png",
over="winx.png",
width=1280, height=780,
onRelease = onBtnRelease
}
winImage.y = ch\*0.5
winImage.isVisible = false
--object fillcolors
--group inserts, including group:insert( winImage )
--addBody properties
startImage.touch = tapToStart
ball.isSleepingAllowed = false;
end
-- ENTER SCENE
function scene:enterScene( event )
local function onAccelerate( event )
if (ball.x \> 600 and ball.x \< 720 and ball.y \> ch-95) then
physics.stop()
winImage.isVisible = true
else
physics.setGravity (0+(event.yGravity\*-15), 10)
end
end
system.setIdleTimer( false )
Runtime:addEventListener("accelerometer", onAccelerate);
startImage:addEventListener("touch", startImage);
end
-- EXIT SCENE
function scene:exitScene( event )
--remove listeners
end
-- DESTROY SCENE
function scene:destroyScene( event )
if winImage then
winImage:removeSelf()
winImage = nil
end
-- nil physics
end
--scene event listeners
--------------------------------------------
return scene
In attempt 2 the widget actually works and responds to my touch, and switches picture to “winx.png” but when I release nothing happens. I expect that the problem is somehow connected to the action being called within the accelerometer event but I’m not sure why it doesn’t respond because of that. It works perfectly if I make the “tapToStart” function call a scene change.
Advice is much appreciated! I trimmed most of the repetitive code so it’s shorter and more readable.
-Pierre [import]uid: 56195 topic_id: 33702 reply_id: 333702[/import]