Corona SDK Touch Phases - Touch and Hold - coronasdk

I am trying to make a game in Corona where if I tap and hold the screen the wheel will rotate but it only rotates when i keep tapping the screen how to I change this so that as long as my finger is pressed on the screen, the wheel will rotate? Here is my code:
local physics = require "physics"
physics.start()
--Variables
--[bike = display.newImage("bike.png")
--bike.x = 70
--bike.y = 290
--physics.addBody(bike, {friction = 0.3, bounce = 0.2})
wheel1 = display.newImage("wheel.png")
wheel1.x = 480 / 2
wheel1.y = 320 / 2
wheel2 = display.newImage("wheel.png")
wheel2.x = 480 / 2 + 50
wheel2.y = 320 / 2 - 50
driveBtn = display.newImage("drive.png")
local function driveFunction( event )
wheel1.rotation = wheel1.rotation + 3
wheel2.rotation = wheel2.rotation + 3
end
Runtime:addEventListener("touch", driveFunction)

Here is a very simple solution. By the way, its very good that you started to developing in an early age, keep coding^^ If you dont understand the code below or logic behind it, just post a comment and ask.
local physics = require "physics"
physics.start()
--Variables
--[bike = display.newImage("bike.png")
--bike.x = 70
--bike.y = 290
--physics.addBody(bike, {friction = 0.3, bounce = 0.2})
local wheel1 = display.newImage("wheel.png")
wheel1.x = 480 / 2
wheel1.y = 320 / 2
local wheel2 = display.newImage("wheel.png")
wheel2.x = 480 / 2 + 50
wheel2.y = 320 / 2 - 50
local driveBtn = display.newImage("drive.png")
local function rotateWheel()
wheel1.rotation = ( wheel1.rotation + 3 ) % 360
wheel2.rotation = ( wheel2.rotation + 3 ) % 360
end
local function driveFunction( event )
if event.phase == "began" then
display.getCurrentStage():setFocus( wheel1 )
wheel1.isFocus = true
Runtime:addEventListener( "enterFrame", rotateWheel )
elseif wheel1.isFocus then
if event.phase == "moved" then
elseif event.phase == "ended" then
Runtime:removeEventListener( "enterFrame", rotateWheel )
display.getCurrentStage():setFocus( nil )
wheel1.isFocus = false
end
end
end
Runtime:addEventListener( "touch", driveFunction )

Related

Attempt to perform arithmetic on field 'y' (a nil value)

I'm new here and I'm struggling with this error in Corona, when game ends (lives=0) and I try to remove the background (that is moving with a function "move"):
"Attempt to perform arithmetic on field 'y' (a nil value)"
in line "background.y = background.y + 4"
Is there anybody who can explain me what is the mistake?
THE CODE:
--add PHYSICS
local physics = require( "physics" )
physics.start()
physics.setGravity( 0, 0 )
local lives = 1
local died = false
--###
--add background
background = display.newImageRect( "background.png", 800, 14000 )
background.x = display.contentCenterX
background.y = 730
background.myName = "background"
--add bottle
bottiglia = display.newImageRect( "bottiglia.png", 41, 104 )
physics.addBody( bottiglia, "dynamic", { radius=45, bounce=0.5 } )
bottiglia.x = display.contentCenterX
bottiglia.y = 10
bottiglia.myName = "bottiglia"
--function move
local function move()
bottiglia.y = bottiglia.y + 4
background.y = background.y + 4
end
Runtime:addEventListener( "enterFrame", move )
--###
--add player
studente = display.newImageRect( "studente.png", 98, 79 )
studente.x = display.contentCenterX
studente.y = display.contentHeight - 100
physics.addBody( studente, { radius=40, isSensor=true } )
studente.myName = "studente"
--###
--function collision
local function onCollision( event )
if ( event.phase == "began" ) then
local obj1 = event.object1
local obj2 = event.object2
if ( ( obj1.myName == "studente" and obj2.myName == "bottiglia" ) or
( obj1.myName == "bottiglia" and obj2.myName == "studente" ) )
then
if ( died == false ) then
died = true
-- lives update
lives = lives - 1
livesText.text = "Lives: " .. lives
if ( lives == 0 ) then
display.remove( studente )
display.remove( background)
timer.performWithDelay( 100, endGame )
end
else
studente.alpha = 0
timer.performWithDelay( 500, restoreStudente )
end
end
end
end
Runtime:addEventListener( "collision", onCollision )
livesText = display.newText( "Lives: " .. lives, 200, 80, native.systemFont, 36 )
--thank you all
The Runtime listener (move function) is working all the time. It changes position of bottiglia and background objects but since background does not exist any more you get an error.
A simple solution is to remove the global listener using Runtime:removeEventListener() before you remove the background object.
Use Runtime:removeEventListener("enterFrame", move)
If you don't want remove listener, you can add checks for nil:
--function move
local function move()
if (bottiglia ~= nil and bottiglia.y ~= nil) then
bottiglia.y = bottiglia.y + 4
end
if (background~= nil and background.y ~= nil) then
background.y = background.y + 4
end
end
This is also pretty risky to use such global variables: bottiglia and background.
You can make it little safer if make them as (or something like that):
myGlobalsVars = { }
myGlobalsVars.myGlobalsVars = display.newGroup()
myGlobalsVars.background = display.newGroup()

In Corona SDK, How do I get an object to keep moving while touching the background screen?

Here's the situation. I want a square object to continuously move in y-direction as long as I'm touching the screen. Based on the code, I have it should give me a continuous movement of the square object, but, only when I'm sliding with my finger on the touch screen, the object moves. I want the square object to continuously move when I'm touching the screen on the same spot.
I tried using both timer and runtime event listener to no avail. What's the fix here?
_W = display.contentWidth
_H = display.contentHeight
local background = display.newRect(0,0,_W,_H)
background.anchorX = 0
background.anchorY = 0
local squareTimer
local function squareMoveUp()
square.y = square.y - 5
end
local holding = 'false'
local function startMove(event)
if event.phase == 'began' then
--Runtime:addEventListener('enterFrame',squareMoveUp)
squareTimer = timer.performWithDelay(200,squareMoveUp,0)
elseif event.phase == 'ended' then
--Runtime:removeEventListener('enterFrame',squareMoveUp)
end
return true
end
background:addEventListener('touch',squareMoveUp)
This works just replace square with your object:
_W = display.contentWidth
_H = display.contentHeight
local background = display.newRect(0,0,_W,_H)
background.anchorX = 0
background.anchorY = 0
local square = display.newRect(200,200,_W/3,_H/3)
square:setFillColor( 1,1,0 )
local squareTimer
local touchStarted = false
local holding = 'false'
local function startMove(event)
if event.phase == 'began' then
--Runtime:addEventListener('enterFrame',squareMoveUp)
squareTimer = timer.performWithDelay(200,squareMoveUp,0)
touchStarted = true
local function squareMoveUp( )
square.y = square.y - 0.02
end
for i = 1,1000 do
if touchStarted then
squareTimer = timer.performWithDelay(200,squareMoveUp,1)
end
end
elseif ( event.phase == "ended" ) then
touchStarted = false
end
return true
end
background:addEventListener('touch',startMove)
You need to change your code like in the below.
_W = display.contentWidth
_H = display.contentHeight
local background = display.newRect(0,0,_W,_H)
background.anchorX = 0
background.anchorY = 0
local square = display.newRect(200,200,_W/3,_H/3)
square:setFillColor( 1,1,0 )
local touchStarted = false
local function startMove(event)
local function squareMoveUp( )
if touchStarted then
square.y = square.y - 10
end
end
if event.phase == 'began' then
touchStarted = true
elseif ( event.phase == "ended" ) then
touchStarted = false
end
timer.performWithDelay(200,squareMoveUp,0)
end
background:addEventListener('touch',startMove)
or you can change the code in the squareMoveUp such as: transition.moveTo( square, { y=square.y-10, 1 } ) instead of square.y = square.y - 10
it will be more smooth.

How to fix ghost collision issue between a static player object and falling objects in Corona SDK

I am having a strange issue with Corona SDK. I have an egg style catching game where you collect items and avoid certain objects. The player is moved left and right with an accelerometer and objects fall from the sky. The game runs well, except for a major flaw: when the dangerous objects hit the initial player's location (where the player is spawned - x = 160, y = display.contentHeight - 40), collision is detected among the player and the object. As a result there is a ghost effect happening when objects drop to x = 160 (+ or - the width of the player / 2).
The player object:
local createChar = function()
charObject = display.newImageRect( "charSprite.png", y/9, y/9 )
physics.addBody( charObject, "static", { density=1.0, bounce=0.4, friction=0.15, radius=20, shape=basketShape} )
charObject.x = -100; charObject.y = y - ground.contentHeight - y/22
charObject.rotation = 0
charObject.isHit = false
charObject.myName = "character"
fishDead = display.newImageRect( "fishdead.png", y/20, y/20 )
fishDead.alpha = 1.0
fishDead.isVisible = false
gameGroup:insert( charObject )
end
The dangerous sea lion object is created through a timer (every 3 seconds). This is the code for each time interval
local sealionDrop = function()
if gameIsActive == true then
local sealion = display.newImageRect( "sealion.png", y / 11, y / 11 )
sealion.x = 40 + mRand( x - 55 ); sealion.y = -100
sealion.isHit = false
physics.addBody( sealion, "dynamic",{ density=20, bounce=0, friction=1, shape=sealionShape } )
sealion.isFixedRotation = true
gameGroup:insert( sealion )
sealion.gravityScale = sealionSpeed
sealion.postCollision = onLionCollision
sealion:addEventListener( "postCollision", sealion )
end
end
Here is my collision code between the player and the dangerous "sealion" object:
local onLionCollision = function( self, event )
if event.other.myName == "character" and charObject.x == self.x then
hit = timer.performWithDelay( 1, characterHit, 2 )
livesCount()
self.isHit = true
self.parent:remove( self )
self = nil
elseif event.other.myName == "ground" then
print("ground hit")
self.isHit = true
self.parent:remove( self )
self = nil
end
if gameLives < 1 then
timer.cancel( startLionDrop )
print("timer cancelled")
end
end
If someone could help figure out this weird ghost effect, that would be amazing. Thank you!

How to change the direction (angle) of bullet after collision in the direction of motion in corona sdk

I want to change the bullet angle after collision, it should change the direction in which it is moving after collision, and must stop rotating after collision.... please give any suggestions... thanks
Sample code is written below
local physics = require("physics")
physics.start()
local obj
physics.setScale( 60 )
physics.setGravity( 0, 0 )
display.setStatusBar( display.HiddenStatusBar )
local obstacle = display.newCircle( 250, 250, 60 )
obstacle.x = display.contentWidth/2; obstacle.y = 200
physics.addBody( obstacle, { density=150, friction=0.2, bounce=0} )
obstacle.isBullet=true
obj = display.newRect(0, 0,20,40)
obj.x = display.contentWidth/2; obj.y = 780
obj.isBullet = true
obj.color = "white"
physics.addBody( obj, { density=1, friction=0.4, bounce=0.1} )
local target = display.newCircle( 250, 250, 60 )
target.x = obj.x; target.y = obj.y; target.alpha = 0
local function Shot( event )
local t = event.target
local phase = event.phase
if "began" == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t:setLinearVelocity( 0, 0 )
t.angularVelocity = 0
target.x = t.x
target.y = t.y
startRotation = function()
target.rotation = target.rotation + 4
end
Runtime:addEventListener( "enterFrame", startRotation )
local showTarget = transition.to( target, { alpha=0.4, xScale=0.4, yScale=0.4, time=200 } )
myLine = nil
elseif t.isFocus then
if "moved" == phase then
if ( myLine ) then
myLine.parent:remove( myLine ) -- erase previous line, if any
end
myLine = display.newLine( t.x,t.y, event.x,event.y )
myLine:setColor( 255, 255, 255, 50 )
myLine.width = 8
elseif "ended" == phase or "cancelled" == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
local stopRotation = function()
Runtime:removeEventListener( "enterFrame", startRotation )
end
local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } )
if ( myLine ) then
myLine.parent:remove( myLine )
end
t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )
end
end
return true
end
obj:addEventListener( "touch", Shot)
local physics = require "physics"
physics.start()
local rect1 = display.newRect(480,0,10,320)
physics.addBody(rect1, "static")
local rect2 = display.newRect(20,100,20,20)
local function shot()
physics.addBody(rect2, "dynamic")
rect2:applyForce( 1000, 0, rect2.x, rect2.y )
rect2.myName = "rect2"
end
rect2:addEventListener("touch", shot)

Corona SDK Lua- Collision with coin

I want to make a sprite image have a collision with the coin.
The coin is a image and it is always moving because of this code:
local tPrevious = system.getTimer();
local function move(event)
local tDelta = event.time - tPrevious;
tPrevious = event.time;
local xOffset = (0.3 * tDelta );
grass.x = grass.x - xOffset;
grass2.x = grass2.x - xOffset;
coin.x = coin.x - xOffset;
if (grass.x + grass.contentWidth) < 0 then
grass:translate( 480 * 2, 0);
end
if (grass2.x + grass2.contentWidth) < 0 then
grass2:translate( 480 * 2, 0);
end
if (coin.x + coin.contentWidth) < 0 then
coin:translate( 480 * 2, 0);
coinRect.x = coin.x
end
local i;
end
Does anyone know how I can have a collision with the coin image?
Thanks in advance.
Add collision event listener to the coin like this,
local function onLocalCollision( self, event )
if ( event.phase == "began" ) then
print("on collision began")
elseif ( event.phase == "ended" ) then
print( "on collision ended")
end
end
coin:addEventListener( "collision", onLocalCollision)
if you wants the specific collision with the coin and sprite object,you should use collision filter.

Resources