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.
Related
I was wondering if there was a way to stop an object from moving off the screen. In my code, I have a controllable character (player) and I want to prevent it from moving to the left when it's x coordinate is < 1
I have tried to do this in the code below, however, the player will not be stopped if you hold down the left arrow key.
Is there a way to fix this? If I had a guess, I would somehow need my program to continuously check for the case where player.x < 1
motionx = 0; -- Variable used to move character along x axis
speed = 10; -- Set Walking Speed
local function moveplayer (event)
player.x = player.x + motionx;
end
Runtime:addEventListener("enterFrame", moveplayer)
local function onKeyEvent( event )
if ( event.phase == "down" ) then
if ( event.keyName == "left" ) and player.x > 1 then
motionx = -speed
return true
elseif ( event.keyName == "right" ) then
motionx = speed
return true
end
end
end
Runtime:addEventListener( "key", onKeyEvent )
You need to check the boundaries and set the motionx to zero:
local player = display.newCircle(display.contentCenterX, display.contentCenterY, 10)
local motionx = 0; -- Variable used to move character along x axis
local speed = 10; -- Set Walking Speed
local function moveplayer( event )
player.x = player.x + motionx;
if player.x < 0 then
player.x = 0
motionx = 0
elseif player.x > display.contentWidth then
player.x = display.contentWidth
motionx = 0
end
end
Runtime:addEventListener( "enterFrame", moveplayer )
local function onKeyEvent( event )
if event.phase == "down" then
if event.keyName == "left" then
motionx = -speed
return true
elseif event.keyName == "right" then
motionx = speed
return true
end
end
end
Runtime:addEventListener( "key", onKeyEvent )
Simply implement a function that checks your objects position vs your screen boundaries. Whenever it reaches the border of your screen you stop it.
To check your objects position for every frame add your function as an event listener for the event "enterFrame".
Read this for details:
https://docs.coronalabs.com/guide/events/detectEvents/index.html
Of course you can also use the collision engine by placing invisible walls around your screen so your object will bounce back whenever it hits the border.
You were fairly close but may need your whole code to test it. I only changed this player.x < 1 and moved )
motionx = 0; -- Variable used to move character along x axis
speed = 10; -- Set Walking Speed
local function moveplayer (event)
player.x = player.x + motionx;
end
Runtime:addEventListener("enterFrame", moveplayer)
local function onKeyEvent( event )
if ( event.phase == "down" ) then
if ( event.keyName == "left" and player.x < 1) then
motionx = -speed
return true
elseif ( event.keyName == "right" ) then
motionx = speed
return true
end
end
end
Runtime:addEventListener( "key", onKeyEvent )
I am working on a game that involves drawing lines to guide a physics ball into a bucket at the bottom of the screen. However, sometimes the user might draw the lines poorly, and the balls will become stuck. To rid this I would like to check the position of the balls every 3 seconds.
This is what I thought would work:
function checkBallVelocity(event)
startX = event.x
startY = event.y
timer.performWithDelay( 5000, function()
endX = event.x
endY = event.y
print(startX..","..startY.." || "..endX..","..endY)
if startX == endX or startY == endY then
if event.other.name == "1" then
circle1:removeSelf( )
circle1 = nil
ballsMissed = ballsMissed + 1
elseif event.other.name == "2" then
circle2:removeSelf( )
circle2 = nil
ballsMissed = ballsMissed + 1
elseif event.other.name == "3" then
circle3:removeSelf( )
circle3 = nil
ballsMissed = ballsMissed + 1
end
return 1
else
checkBallVelocity()
end
end
)
end
Sadly, it doesn't. Any help/advice would be welcome
There are many ways to do this sort of thing, but Corona provides an enterFrame event which fires each time a frame is drawn (30 or 60 frames per second). It can be used as a main loop to perform periodic actions, but keep in mind that it is run quite often so it helps to define a period and limit your actions to certain phases within the period.
local period
local phase = 0
function mainLoop(event)
phase = phase + 1
if phase == 1 then
checkBallPosition(...)
elseif phase == period
phase = 0
end
end
...
function scene:enterScene( event )
period = display.fps * 3 -- # of seconds
Runtime:addEventListener("enterFrame", mainLoop)
...
end
I can move the box by touching it and dragging it along the x-axis, but i like to be able to flick it from one side to the other. is there a simple solution to do this?
local box = display.newRect( 0, 0, 50, 50)
box:setFillColor( math.random(0,255), math.random(0,255), math.random(0,255) )
physics.addBody( box, { density=3.0, friction=0.5 } )
box.gravityScale = 0.0
function box:touch( event )
if event.phase == "began" then
self.markX = self.x
elseif event.phase == "moved" then
local x = (event.x - event.xStart) + self.markX
self.x = x
end
return true
end
box:addEventListener( "touch", box )
You could use transition.to, but you'll have to decide by how much to move. I'll assume you want to move it all the way to other side:
function box:touch( event )
if event.phase == "began" then
self.markX = self.x
elseif event.phase == "end" then
local targetX = 0 -- if flick left
if event.x > self.markX then -- flick right
targetX = display.contentWidth - self.width
end
local duration = 1000 -- 1 second
transition.to(self, {duration, x=targetX})
end
return true
end
Have not tested, could be syntax or other errors, but this should give you a good idea of how to proceed (if not put a comment).
I followed this tutorial to make a breakout game, but at some point, the ball keeps leaning on the top wall when the ball angle is too large ( too horizontal ). Is there any logic I can tune so that the ball can avoid this behavior?
Here is the screenshot :
The ball's related source code is:
local ballRadius = 10
ball = display.newCircle( display.contentWidth / 2, display.contentHeight / 2, ballRadius )
physics.addBody(ball, "dynamic", {friction=0, bounce = 1, radius=ballRadius})
It is some kind of weird. But I've done once like this...
Create 3 variables/flags:
local horizontalMotionFlag,yPos_1,yPos_2 = 0,0,0
Then:
wall.type = "LeftWall" -- to the LeftWall
-- and --
wall.type = "RightWall" -- to the RightWall
Add the following lines inside event.phase == "ended"
------------------------------------------------------------------------------------------
if(event.other.type == "LeftWall") then
yPos_1 = ball.y
if(horizontalMotionFlag==0)then
horizontalMotionFlag = 1
else
if(math.abs(yPos_1-yPos_2) < 50)then
print("CoHorizontal motion detected. Change angle...1")
horizontalMotionFlag = 0
ball:applyForce( 0, 1, ball.x, ball.y ) -- apply a small downward force
ball:applyForce( 0, 0, ball.x, ball.y ) -- resetting the force
-- You can also check the direction of ball and apply force to -1(upwards) also --
end
end
end
if(event.other.type == "RightWall") then
yPos_2 = ball.y
if(horizontalMotionFlag==0)then
horizontalMotionFlag = 1
else
if(math.abs(yPos_1-yPos_2) < 50)then
print("CoHorizontal motion detected. Change angle...")
horizontalMotionFlag = 0
ball:applyForce( 0, 1, ball.x, ball.y ) -- apply a small downward force
ball:applyForce( 0, 0, ball.x, ball.y ) -- resetting the force
-- You can also check the direction of ball and apply force to -1(upwards) also --
end
end
end
------------------------------------------------------------------------------------------
Then add the following line inside event.other.type == "destructible" and event.other.type == "bottomWall" :
horizontalMotionFlag = 0;
Keep Coding............. :)
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 )