HowTo create a fuel bar - lua

I would like to build a fuel bar (from full tank to empty). When the user touch the screen, the fuel bar dicrease.
Here is my code :
lifeBar = display.newImage("fuel_bar1.png")
lifeBar.anchorX=0
lifeBar.anchorY=0.6
lifeBar.x = fuel_title.x +114+13.5
lifeBar.y = 37
screenGroup:insert(lifeBar)
Then, i used a function called FuelConsumption() to reduce the fuel tank each time the variable "pressed" is equal to "true". When Pressed==true, it means that the user is touching the screen :
function FuelConsumption()
if lives > 0 and pressed==true then
lives = lives - 1
lifeBar.width=lifeBar.width-1
livesValue.text = string.format("%d", lives)
end
end
This function is active on the enterFrame event as follow :
Runtime:addEventListener( "enterFrame", FuelConsumption )
It works very well, but my question is : How is possible to reduce the lifebar width each 5 secondes (once the variable pressed==true) ?
I tried to add this to my touch event function :
function flyUp(event)
if event.phase == "began" then
pressed=true
if gameStarted == false then
gameStarted = true
pauseBtn.isVisible=true
opt_btn.isVisible=true
Runtime:addEventListener("enterFrame", enterFrameListener)
Runtime:addEventListener("enterFrame",scrollGrasses)
timer.performWithDelay( 5000, function() Runtime:addEventListener( "enterFrame", FuelConsumption ) end,-1 )
end
elseif event.phase == "ended" then
pressed = false
timer.performWithDelay( 1000, function() Runtime:removeEventListener( "enterFrame", FuelConsumption ) end,-1 )
end
end
The problem is that code is consumming a lot of memory and the game is going so slow ! So, is there any other way to do ?
Thank you :)

Instead of using a single image use a sprite sheet.That might boost up the game.

Related

Corona SDK While loop crash

What is wrong with this code that it crashes the simulator?
I'm new to Corona SDK, but I know alot of Lua from Roblox.
local x,y,touching,active = 2,2,false,true
local background = display.newImage("Icon.png",(display.pixelWidth/2)+30,y)
background:scale(60,60)
print("Started")
function move()
y=y+1
end
function onObjectTouch(event)
if event.phase == "began" then
touching = true
while touching == true do
timer.performWithDelay( 1000, move)
end
elseif event.phase == "ended" then
touching = false
end
return true
end
background:addEventListener("touch",onObjectTouch)
Corona is event based. You essentially lock up the touch event in a while loop which prevents the ended phase from triggering to break your endless loop.
Corona has a Runtime event called "enterframe". It is called 30-60 times a second (probably less depending on the work it is doing) that is essentially an update method.
Using your logic:
function onObjectTouch(event)
if event.phase == "began" then
touching = true
elseif event.phase == "ended" then
touching = false
end
end
funtion EnterFrame(event)
if touching == true then
timer.performWithDelay( 1000, move)
end
end
There are other ways to accomplish this by setting a dynamic objects velocity when the touch is started and setting that velocity to 0 when the touch ends.

Can someone test my Corona project and identify the issue with my jump function?

I'm all very new to this and everything in this project is just placeholders and scrap work. Some of you may recognize some of the graphics used from other Corona tutorials, but it's to help me get better, so don't be too judgemental. Here's a link to the download for the Corona Simulator: http://www.mediafire.com/download/6a78bsewgwsiyp2/GooMan.rar
Anyways, here's my issue. The jump button seems fine at first. If I hold it down, the character constantly jumps. And if I let go, he stops. If I simultaneously hold down jump while pushing one of the arrow buttons, he'll jump in that direction. However, it seems as though if I jump once, then I hit the jump button again RIGHT as the character is making contact with the ground, that he won't jump. There's a sudden lack of responsiveness. I literally have to pause slightly and wait for the character to fully hit the ground before I can make another successful jump. Why?
And here's all the relevant code for you to look at:
I have this at the beginning to help define my goo character's position:
local spriteInAir = false
local yspeed = 0
local oldypos = 0
local holding = false
I then created the jump button.
local bJump = display.newImage("Images/jumpbutton.png")
bJump.x = 445
bJump.y = 265
bJump.xScale = 0.78
bJump.yScale = 0.78
Followed up by creating an enterFrame Runtime Event which will update my goo character's position in the Y. Running the game at 60fps if that's relevant.
function checkSpeed()
yspeed = sprite.y - oldypos
oldypos = sprite.y
if yspeed == 0 then
spriteInAir = false
else
spriteInAir = true
end
end
Runtime:addEventListener( "enterFrame", checkSpeed )
Then the meat of it all. Created a function called hold which tells the game to not only make my goo character jump, but to keep my goo character constantly jumping as long as the bJump button is held down. Works perfectly. The "jumping" function is a touch event that listens for the hold function, and all of it is executed by the bJump button listening to the jumping function.
local function hold()
if holding and spriteInAir == false then
sprite:applyForce( 0, -8, sprite.x, sprite.y )
sprite:setLinearVelocity(0, -350)
spriteInAir = true
return true
end
end
local function jumping( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
event.target.alpha = 0.6
Runtime:addEventListener( "enterFrame", hold )
holding = true
elseif event.target.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
holding = false
event.target.alpha = 1
Runtime:removeEventListener( "enterFrame", hold )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
spriteInAir = false
return true
end
end
return true
end
bJump:addEventListener("touch",jumping)
Anyone who can help me identify this problem, I'd greatly appreciate it!
You're using a velocity check to detect if the character is on the ground. It can take a short while to set it back to zero after colliding with Box2D, so the better way to do it is to use a collision sensor:
sprite.grounded = 0 -- Use a number to detect if the sprite is on the ground; a Boolean fails if the sprite hits two ground tiles at once
function sprite:collision(event)
if "began" == event.phase then
-- If the other object is a ground object and the character is above it...
if event.other.isGround and self.contentBounds.yMax < event.other.contentBounds.yMin + 5 then
sprite.grounded = sprite.grounded + 1 -- ...register a ground collision
end
elseif "ended" == event.phase then
-- If the other object is a ground object and the character is above it...
if event.other.isGround and self.contentBounds.yMax < event.other.contentBounds.yMin + 5 then
sprite.grounded = sprite.grounded - 1 -- ...unregister a ground collision
end
end
end
sprite:addEventListener("collision")
Then, in your jump function, just check to see if sprite.grounded > 0. If it is, the player is grounded.

How do I make a collison while someone is holding a button

So I have this cube which if the player clicks on a button moves. I also have this block which if the cube collides with, the cube will get sent back to a start position. I have tried my best but I can't seem to get both of them working correctly together. With my code, the cube get's glitchy and moves all over the screen between the block and start position. This is if the button is still pressed. If it is released at the same moment of collision it does work, but obviously the players won't be paying attention to that.
function touchHandler( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
Runtime:addEventListener( "enterFrame", enterFrameListener )
holding = true
elseif event.target.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
holding = false
Runtime:removeEventListener( "enterFrame", enterFrameListener )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
leftbutton:addEventListener( "touch", touchHandler )
This is the code for the collision:
function onCollision( event )
if ( event.phase == "began" ) then
transition.cancel( )
transition.moveTo( cube, {time = 0, x = 35, y = 35} )
end
return true
end
redblock:addEventListener( "collision", onCollision )
Also: whenever the cube falls on an edge of the redblock and starts spinning and gets sent back to start. It keeps spinning and it starts moving on its own.
I hope someone can help!
Thanks.
You are still in the "moved" phase during the collision hence why the block tries to reset, but is then dragged around afterwards. Once you detect the collision, you need to remove the event handler from the "leftbutton" so that the touch event is no longer in progress.

Touch Hold Event In corona SDK

I was wondering how I would check if the user has touched the screen, but is holding their touch down and is not moving. Please help if you have anything I can go from. Ive been looking around and have yet to find anything to handle this.
You can use/modify this: (It's what Rob Miracle says)
local holding = false
local function enterFrameListener()
if holding then
-- Holding button
-- Code here
-- Code here
-- Code here
else
-- Not holding
-- Code here
-- Code here
-- Code here
end
end
local function touchHandler( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( event.target )
event.target.isFocus = true
Runtime:addEventListener( "enterFrame", enterFrameListener )
holding = true
elseif event.target.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
holding = false
Runtime:removeEventListener( "enterFrame", enterFrameListener )
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false
end
end
return true
end
I believe its obvious what touchHandler function is ^^
You will need a touch listener added to either an object covering your whole screen or you can add it to the system's RunTime.
See this guide: http://docs.coronalabs.com/guide/events/detectEvents/index.html#hit
Now, there are three "Phases" for these touch events. You get one when the press starts ("began"), one if the person moves their finger ("moved") and when they stop touching, there is an "ended" phase.
Depending on what you are trying to do, if you are say moving something while holding the button down, then you can set a flag like:
if event.phase == "began" then
pressed = true
elseif event.phase == "ended" then
pressed = false
end
Then where ever you're moving you can check to see "if pressed then move".

Button gotoScene not working

the storyboard.gotoScene("facebook", "fade", 400) is not working if i tap the button, and i dont get any error messages in the terminal. What am i doing wrong ?
-- requires
display.setStatusBar( display.HiddenStatusBar )
_W = display.contentWidth; --Returns Screen Width
_H = display.contentHeight; --Returns Screen Height
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
-- background
function scene:createScene(event)
local screenGroup = self.view
background = display.newImage("restart.png")
screenGroup:insert(background)
button = display.newImage("share2.png")
button.x = display.contentWidth / 2
button.y = display.contentHeight -400
end
function listener(event)
if event.phase == "began" then
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end
end
function scene:enterScene(event)
storyboard.purgeScene("game")
button:addEventListener( "tap", listener )
end
function scene:exitScene(event)
button:removeEventListener( "tap", listener )
end
function scene:destroyScene(event)
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
The "tap" event and "touch" events are different and they get different "phases" passed to the event handler. The way you have your event handler programmed, you're expecting "touch" events (began, ended, moved). The tap event doesn't really generate any phases, either you were tapped or not.
Either change these two lines:
button:addEventListener( "tap", listener )
button:removeEventListener( "tap", listener )
to:
button:addEventListener( "touch", listener )
and
button:removeEventListener( "touch", listener )
or you can change your listner to:
function listener(event)
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end
Try this:
storyboard.gotoScene("facebook", {effect = "fade", time=400})
Or:
local options =
{
effect = "fade",
time = 400,
}
storyboard.gotoScene("facebook", options)
http://docs.coronalabs.com/api/library/storyboard/gotoScene.html
It looks like corona does not like it when you call a Scene 'facebook' or renamed facebook to 'postmyscore' and it works
I had the same problem since Coronas last update. I fixed the problem by removing the if check for the event phase. Just comment out the event.phase check in your listener function:
function listener(event)
--if event.phase == "began" then
print(event.name.." occurred")
storyboard.gotoScene("facebook", "fade", 400)
end
facebook.lua is already integrated in the inbuild API , so it may generate the problem , more over you are using tap event , so in listener no need to check the phase or you may use touch event.

Resources