Hey everyone!
Something strange has happened with my app, it seems like applyForce apples to much force. It has worked until now but now it just speeds up to a velocity to -1000 then i click the buttonUp.
You don’t even see the sprite moving it just disappear from screen:S
i post the entire lua file hope thats ok?
[lua]module(…, package.seeall)
–import of modules
local sprites = require(“sprite”)
function new()
local localGroup = display.newGroup()
local gr = display.newGroup ( )
local gr2 = display.newGroup ( )
–Creation of physics engine and some other settings
local physics = require(“physics”)
physics.start()
physics.setGravity(0,5,0)
physics.setDrawMode(“normal”)
display.setStatusBar (display.HiddenStatusBar)
–Creating the Ufo with a spritesheet
local zwoptexData = require (“testsheet2”)
local data = zwoptexData.getSpriteSheetData()
local spriteSheet = sprite.newSpriteSheetFromData( “testsheet2.png”, data )
local spriteSet = sprite.newSpriteSet(spriteSheet,1,29)
local spriteInstance = sprite.newSprite(spriteSet)
–Additional settings for the spritesheet
sprite.add(spriteSet, “ufo”, 1, 1, 300, 0)
sprite.add(spriteSet, “explosion”, 2, 29, 300, 1)
spriteInstance.x = 200
spriteInstance.y = 0
–Prepare and play the spritesheet
spriteInstance:prepare(“ufo”)
spriteInstance:play(“ufo”)
–Adding the Ufo to the physics engine
physics.addBody(spriteInstance, {density = 1.0, friction = 0.3, bounce = 0})
–Setting up the button up
local buttonUp = display.newRect(0, 0, 480, 320)
buttonUp.name = “buttonUp”
buttonUp.isHitTestable = true
buttonUp.isVisible = false
gr2:insert(buttonUp)
–Setting up the button left
local buttonLeft = display.newRect(20, 40, 40, 40)
buttonLeft.name = “left”
gr:insert(buttonLeft)
–Setting up the button right
local buttonRight = display.newRect(70, 40, 40, 40)
buttonRight.name = “right”
gr:insert(buttonRight)
–Setting up the button pause
local pause = display.newRect(370, 40, 40, 40)
pause.name = “pause”
gr:insert(pause)
–Creating the ground and placing it
local scaleFactor = 1.0
local physicsData = (require “ground_t”).physicsData(scaleFactor)
local ground = display.newImage(“ground_test.png”)
ground.name = “ground”
ground.y = 200
gr:insert(ground)
–Adding the ground to the physics engine as an body and making it static
physics.addBody( ground, physicsData:get(“ground_test”) )
ground.bodyType = “static”
–Creating the platform where Ufo is intended to land on
–and placing it, also giving it a name
local platform = display.newImage(“platform.png”)
platform.x = 210
platform.y = 140
platform.xScale = 0.5
platform.name = “platform”
gr:insert(platform)
local pauseMenu = display.newRect( 100, 70, 200, 200 )
pauseMenu.name = “pauseMenu”
pauseMenu.isVisible = false
gr:insert(pauseMenu)
–Adding the platform to the physics engine as an body and also making it static
physics.addBody(platform, {density = 0, friction = 0, bounce = 0})
platform.bodyType = “static”
–Creating two variables to determine the place and if the ufo has crashed
local crashedPos = 0
local crashed = false
–A function that checks collisions both if the ufo has landed and if it has crashed
function collisionWithObstacle(event)
vex, vey = spriteInstance:getLinearVelocity()
print(vey)
–An if that checks if the ufo collides with the platform and if
–that is the case it will stop moving and the text “Grattis du landade”
–appears in the middle of the screen
if(event.other.name == “platform” and vey < 100) then
local doCollision = function()
local landat = display.newText(“Grattis du landade!”, 50, 150, native.systemFont, 40)
crashed = true
spriteInstance.linearDamping = 100,0
end
elseif (event.other.name == “platform” and vey > 100) then
local doCollision = function()
spriteInstance.isBodyActive = false
spriteInstance:prepare(“explosion”)
spriteInstance:play(“explosion”)
end
–An elseif that checks if the ufo collides with the gorund if it does then the
–ufo will disappear and an explosion will appear
elseif (event.other.name == “ground”) then
local doCollision = function()
spriteInstance.isBodyActive = false
spriteInstance:prepare(“explosion”)
spriteInstance:play(“explosion”)
end
local collisionTimer = timer.performWithDelay(1, doCollision, 1)
end
end
–An listener who listens after collisons
spriteInstance:addEventListener(“collision”, collisionWithObstacle)
function animationEnded(event)
if(not spriteInstance.animating)then
spriteInstance.isVisible = false
end
end
spriteInstance:addEventListener(“sprite”, animationEnded)
–A function the controls the ufos movement
function ufoMovement(event)
–An if that controls what happens then user hit anywhere on the screen and it goes up
if(event.target.name == “buttonUp” and crashed == false) then
–An if that fires an timer then touch began
if(event.phase == “began”) then
print ( “klickup” )
tmrUp = timer.performWithDelay(150, engines, 0)
–An elseif that checks then user stops to touch the button
elseif(event.phase == “ended”) then
if(tmrUp ~= nil)then
timer.cancel(tmrUp)
print ( “klickner” )
end
spriteInstance.linearDamping = 0
end
end
–An if that controls what happens then user hit the left button and it goes left
if(event.target.name == “left” and crashed == false) then
–An if that fires an timer then touch began
if(event.phase == “began”) then
tmrLeft = timer.performWithDelay(150, sideLeft, 0)
–An elseif that checks then user stops to touch the button
elseif(event.phase == “ended”) then
if(tmrLeft ~= nil)then
timer.cancel(tmrLeft)
end
end
end
–An if that controls what happens then user hit the right button and it goes right
if(event.target.name == “right” and crashed == false) then
–An if that fires an timer then touch began
if(event.phase == “began”) then
tmrRight = timer.performWithDelay(150, sideRight, 0)
elseif(event.phase == “ended”) then
if(tmrRight ~= nil)then
timer.cancel(tmrRight)
end
end
end
return true
end
–Three listeners that listen for touch on the different buttons in the game
buttonUp:addEventListener(“touch”, ufoMovement)
buttonLeft:addEventListener(“touch”, ufoMovement)
buttonRight:addEventListener(“touch”, ufoMovement)
–A function with a timer in that applys force to the sprite up
function engines(event)
spriteInstance.linearDamping = 0.5
spriteInstance:applyForce(0, -30, spriteInstance.x, spriteInstance.y)
end
–A function with a timer in that applys force to the sprite left
function sideLeft(event)
spriteInstance:applyForce(-20, 0, spriteInstance.x, spriteInstance.y)
end
–A function with a timer in that applys force to the sprite right
function sideRight(event)
spriteInstance:applyForce(20, 0, spriteInstance.x, spriteInstance.y)
end
function pauseGame(event)
if(event.target.name == “pause”)then
physics.pause()
pauseMenu.isVisible = true
elseif(event.target.name == “pauseMenu”)then
physics.start()
pauseMenu.isVisible = false
end
return true
end
pause:addEventListener(“touch”, pauseGame)
pauseMenu:addEventListener ( “touch”, pauseGame )
return localGroup
end[/lua]
Kind regards
Inkoqnito [import]uid: 90942 topic_id: 16945 reply_id: 316945[/import]