I have a code
– main.lua
display.setStatusBar( display.HiddenStatusBar )
local physics = require (“physics”)
physics.start()
local bg = display.newImage (“bg.png”)
local cat = display.newImage (“cat.png”,80,10)
cat:scale (.5, .5)
local ground = display.newImage (“hills.png”, 0, display.contentHeight-100)
local leftWall = display.newRect(-10, 0, 13, 500) – note 1
leftWall.strokeWidth = 1
leftWall:setFillColor(0, 255, 0)
leftWall:setStrokeColor(0, 255, 0)
local rightWall = display.newRect(310, 0, 10, 150)
rightWall.strokeWidth = 1
rightWall:setFillColor(0, 255, 0)
rightWall:setStrokeColor(0, 255, 0)
local rightWall2 = display.newRect(310, 140, 10, 180)
rightWall2.strokeWidth = 1
rightWall2:setFillColor(0, 255, 0)
rightWall2:setStrokeColor(0, 255, 0)
local rightWall3 = display.newRect(310, 322, 10, 150)
rightWall3.strokeWidth = 1
rightWall3:setFillColor(0, 255, 0)
rightWall3:setStrokeColor(0, 255, 0)
local topWall = display.newRect(0, 0, 350, 5)
topWall.strokeWidth = 1
topWall:setFillColor(0, 255, 0)
topWall:setStrokeColor(0, 255, 0)
local floor = display.newRect(0, 475, 350, 5)
floor.strokeWidth = 1
floor:setFillColor(0, 255, 0)
floor:setStrokeColor(0, 255, 0)
local myText = display.newText (“Help the cat scape”, 50,0,nil,22)
local function dragBody ( event )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage ( )
if “began” == phase then
stage:setFocus ( body, event.id )
body.isFocus = true
body.tempJoint = physics.newJoint ( “touch”, body, event.x, event.y )
elseif body.isFocus then
if “moved” == phase then
body.tempJoint:setTarget ( event.x, event.y )
elseif “ended” == phase or “cancelled” == phase then
stage:setFocus (body, nil)
body.isFocus = false
body.tempJoint:removeSelf ( )
end
end
return true
end
physics.addBody (leftWall, “static”)
physics.addBody (rightWall, “static”)
physics.addBody (rightWall3, “static”)
physics.addBody (topWall, “static”)
physics.addBody (floor, “static”)
physics.addBody (cat, “dynamic”, {density=1, friction=-0.9 , bounce = 0.3})
cat:addEventListener (“touch”, dragBody)
--------NOTE 1-------
– first number, move object from left to right
– second number, move object from top to bottom
– third number, makes object width bigger
– fourth number, makes object height taller
And works perfect! a nice simple game.
------------------------------------------------------------------THE PROBLEM-----------------------
I want to add this game on one of my storyboard scenes.
– So, where do I place the dragBody (Event) function? – inside the enterScene?, before? Where?
–Where do I put the – physics.addBody? – at the end? after the addEventListeners? Where?
–Where do I put all the images for the walls and the cat? – in the createScene? Where?
This is my complete code, but it doesn’t work.
– --==***************************************************************************+±- –
– Music Theory Game
– By Victor M. Barba
– Copyright 2013 – All Rights Reserved
– Version 1.0
– --==***************************************************************************+±- –
local storyboard = require( “storyboard” )
local widget = require “widget”
local scene = storyboard.newScene()
local wrongSound = audio.loadSound( “wrongSound.mp3”)
local physics = require (“physics”)
physics.start()
– --==******************[FUNCTIONS TO GO TO ANOTHER SCENE]**********************+±- –
local function buttonLearnLevel1()
storyboard.gotoScene( “home”, “crossFade”, 300 )
return true
end
local function dragBody ( event )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage ( )
if “began” == phase then
stage:setFocus ( body, event.id )
body.isFocus = true
body.tempJoint = physics.newJoint ( “touch”, body, event.x, event.y )
elseif body.isFocus then
if “moved” == phase then
body.tempJoint:setTarget ( event.x, event.y )
elseif “ended” == phase or “cancelled” == phase then
stage:setFocus (body, nil)
body.isFocus = false
body.tempJoint:removeSelf ( )
end
end
return true
end
– --==**************************[CREATE SCENE]**********************************+±- –
function scene:createScene( event )
local group = self.view
local background = display.newImage( “backgroundPlayCat.png” )
background:scale (1.03, 1.03)
buttonLearnLevel1 = widget.newButton{
defaultFile=“buttonHome.png”,
onRelease = buttonLearnLevel1
}
buttonLearnLevel1.x = 530
buttonLearnLevel1.y = 680
local wall1 = display.newImage( “wall1red.png” )
wall1.x = 1020
wall1.y = 50
local wall2 = display.newImage( “wall2red.png” )
wall2.x = 500
wall2.y = 5
local wall3 = display.newImage( “wall3red.png” )
wall3.x = 7
wall3.y = 390
local wall4 = display.newImage( “wall4red.png” )
wall4.x = 500
wall4.y = 760
local wall5 = display.newImage( “wall5red.png” )
wall5.x = 1020
wall5.y = 480
local ladyBug = display.newImage( “ladyBug.png” )
ladyBug.x = 100
ladyBug.y = 100
ladyBug:scale (.5, .5)
---------------------------------------------------------------------insert into group----
group:insert ( background )
group:insert ( buttonLearnLevel1 )
group:insert ( wall1 )
group:insert ( wall2 )
group:insert ( wall3 )
group:insert ( wall4 )
group:insert ( wall5 )
end
– --==***************************[ENTER SCENE]**********************************+±- –
function scene:enterScene( event )
local group = self.view
end
– --==***************************[EXIT SCENE]**********************************+±- –
function scene:exitScene( event )
local group = self.view
end
– --==**************************[DESTROY SCENE]*********************************+±- –
function scene:destroyScene( event )
local group = self.view
if buttonLearnLevel1 then
buttonLearnLevel1:removeSelf()
buttonLearnLevel1 = nil
end
end
– --==*************************[EVENT LISTENER]*********************************+±- –
physics.addBody (ladyBug, “dynamic”, {density=1, friction=-0.9 , bounce = 0.3})
ladyBug:addEventListener (“touch”, dragBody)
scene:addEventListener( “createScene”, scene )
scene:addEventListener( “enterScene”, scene )
scene:addEventListener( “exitScene”, scene )
scene:addEventListener( “destroyScene”, scene )
return scene
I hope you can help me with this, this is all I need to finish my app.
Thanks
Victor