In Corona, I need help moving a character to one of three points by swipe - coronasdk

When a user swipes the screen, I need the player to move to specific position. So, if the player is in the center of the screen, and the user swipes left, the x position is -100. If the user swipes right again, the player would move back to center. Another swipe right would put the x position at + 100. A swipe right would do nothing from that point, but a swipe left would move them back to center See below for text visual: Any help would be GREATLY APPRECIATED!
swipe swipe
<----> <---->
X X X

Implementing "swipe" is pretty easy, but you have to understand touch handlers and the different event phases and the data passed to the handler to make a lot of sense for this.
A swipe is nothing more than a touch that moves in a direction and then the app responds to the action. This is similar to dragging, but a drag moves the object with the touch. The basic touch handler is something like this:
local LEFT = 50
local CENTER = display.contentCenterX
local RIGHT = display.contentWidth - 50
local object = display.newCircle( display.contentCenterX, display.contentCenterY, 25 )
local function handleSwipe( event )
if event.phase == "moved" then
local dX = event.x - event.xStart
print(event.x, event.xStart, dX)
if dX > 10 then
-- swipe right
local spot = RIGHT
if event.target.x == LEFT then
spot = CENTER
end
transition.to( event.target, {time=500, x = spot } )
elseif dX < -10 then
-- swipe left
local spot = LEFT
if event.target.x == RIGHT then
spot = CENTER
end
transition.to( event.target, {time=500, x = spot } )
end
end
return true
end
object:addEventListener( "touch", handleSwipe )
Now I used 10 px of touch movement to do this, but I normally use 5px of touch movement, adjust to taste.

Related

3 point object moving is happening all at once

I am currently making a jumping game where there is an object at the left side of the screen, on a platform. Once the object successfully jumps and lands on the platform on the right, it does the following:
1) the platform on the right moves to the left
2) the platform on the left (that you JUST jumped off from) moves off-screen.
3) a new platform is supposed to appear at the right side of the screen, thus continuing the loop.
I have already made the functions where it allows the object to jump and show if the collision is successful or not. My problem is, the 3 things I have mentioned above happens, but it keeps on going and does not stop for the object to make the next jump. This makes the object go off screen as well, since the platforms keep moving towards the left. After debugging, I feel like the problem lies where the collision happens. This is because, once it touches the platform, the collision keeps on happening until the object is off of the platform. I was wondering if you guys can help me with this!
Here is part of my code that is relevant:
local onPlatform = false
local gameStarted = false
function playerCollision( self, event )
if ( event.phase == "began" ) then
--if hit bottom column, u get points
if event.target.type == "player" and event.other.type == "bottomColumn" then
print ("hit column")
onPlatform = true
else
--if hit anything else, gameOver
--composer.gotoScene( "restart" )
print ("hit ground")
end
end
end
function moveColumns()
for a = elements.numChildren,1,-1 do
--speed of columns moving
--if greater than -100, keep moving it to left
--puts platform at the right and stops
if (elements[a].x > display.contentWidth/1.1) then
elements[a].x = elements[a].x - 12
end
--moves platform to left after it successfully lands
if (onPlatform == true) then
if (elements[a].x > display.contentWidth/3 and elements[a].x < display.contentWidth/1.11) then
elements[a].x = elements[a].x - 12
end
end
--moves left platform to off-screen and deletes after it passes a certain X-axis
if (onPlatform == true) then
if(elements[a].x > -100 and elements[a].x < display.contentWidth/2.99) then
elements[a].x = elements[a].x - 12
--adds score if it goes past a certain X-axis at left side
elseif(elements[a].x < -100) then
mydata.score = mydata.score + 1
tb.text = mydata.score
elements[a].scoreAdded = true
elements:remove(elements[a])
elements[a] = nil
end
end
end
end
When are you calling moveColumns?? Could you just call it from within playerCollision and remove the onPlatform variable? Probably need a bit more code to help properly.

How to implement navigation between scene in corona like swipe view in android?

I have three Composer Scene and I like to navigate between them by swiping left or right. What I have tried to do in my listener is to check
if( event.phase == "moved" ) then
local dx = (event.x - event.xStart)
if(dx > 20) then
composer.removeScene("level", false)
composer.gotoScene("wlc", {effect = "slideRight",time = 5000} )
end
end
but in this case when I swipe the scene will be replaced and I can't stop it or go back
and what I need is to control sliding by my finger like ViewPger in android
any way to achieve this?
With Composer, once you start the scene transition, there is no way to abort.

How can I make an image fall down randomly as if meteors in Corona SDK?

What I want is that the images fall from the top side of the screen and start falling down accelerating, they would only be falling straight down, alternating positions around the width of the screen, meaning one on the right, then another one in the middle, and then another one on the left side and so on in the different positions, until they disappear at the bottom of the screen.
I tried this
function moveMeteors()
for i = 1, math.random(1, 2) do
meteors = display.newImage("meteor.png")
screenGroup:insert(meteors)
meteors.x = (math.random(display.contentWidth))
meteors.y = centerY - 340
transition.to(meteors, {time = math.random(3500 - speedMeteor, 4500 - speedMeteor),
y = 1000 + speedMeteor, onComplete = clear })
speedMeteor = speedMeteor + 10
end
end
But, sometimes the images appear one over the other and I do not want that, I mean, each image appear and go from the top to the bottom of the screen in his own line. I hope that I've explained this well.
You should look into utilizing the built in physics of Coronasdk. CoronaDocs:Physics.
As an example this code should easily simulate the effect you a trying to get, you will have to add functions to take care of removing objects as they leave the screen etc.
local physics = require("physics")
physics.start()
function SpawnMeteor()
local meteor = display.newImage( "meteor.png", math.random(display.contentWidth), centerY - 340)
physics.addBody( meteor)
end
timer.performWithDelay( 2000, SpawnMeteor)

Corona SDK arrow to show direction of ball?

I'm new to programming, and I wonder if you can help me out with this problem. I created a simple basketball game, where you pull back then release to fire the ball. I want to know how to use an image of an arrow to show the direction the ball will travel(It doesn't have to be the exact trajectory, just a straight arrow). Also, i want the yScale of the arrow to show how much linear impulse will be applied.Thanks in advance!
here is my function for shooting the ball
local function ballTouched(event)
if ball.x == 100 and ball.y == 100 then
if event.phase == "began" then
display.getCurrentStage():setFocus(ball)
arrow.alpha=1
elseif event.phase == "ended" then
physics.start()
ball:applyLinearImpulse((event.xStart - event.x)/2, (event.yStart - event.y)/2, ball.x, ball.y)
display.getCurrentStage():setFocus(nil)
arrow.alpha=0
man2.alpha=1
man.alpha=0
end
end
end
Runtime:addEventListener("touch", ballTouched)
You should know the angle you are pulling back. Just set the .rotation value of your arrow to the same angle.
arrow.rotation = aimAngle
To follow up to the comments below:
I'm assuming you are pulling the basketball back kind of like AngryBirds slingshot or you have to be touching and dragging somehow to have an angle. You can use some simple angle math where you know the length of two sides of a right triangle and you can use the arctangent function to compute the angle.
local function angleBetween( srcX, srcY, dstX, dstY )
local angle = ( math_deg( math_atan2( dstY-srcY, dstX-srcX ) )+90 )
--if ( angle < 0 ) then angle = angle + 360 end
return angle%360
end
where srcX and srcY would be the position (or start position of the basketball if you're dragging it) and dstX, dstY would be the event.x, event.y of where you let up during the ended phase. The event table has a xStart and yStart members that kept track of where the touch event started, so maybe
aimAngle = angleBetween(event.xStart, event.yStart, event.x, event.y)
You may need to play with some of these numbers to get them lined up the way you want them (i.e. you may need to add 180 or something)

How to rotate an Image using Corona sdk with iPhone

I am a newbie to Corona. I have an image that is draggable over the screen. Now i want to apply rotation to that image object.
The code I currently have is:
myAnim1 = movieclip.newAnim{"ICQ.png"}
--foreground:insert( myAnim2 )
myAnim1.x = 20
myAnim1.y = 80
local function pressFunction()
myAnim1.alpha = 0.7
end
local function releaseFunction()
myAnim1.alpha = 1.0
end
-- Make 2nd sprite draggable
myAnim1:setDrag{
drag=true,
onPress=pressFunction,
onRelease=releaseFunction,
bounds = { 0, 0, 320, 480 }
}
local rotate = function( event )
myAnim1.rotation = event.x
end
myAnim1:addEventListener( "touch",rotate)
In this code the image rotates while I drag it. I want rotation to happen after dropping the image at some place on the screen.
Can anyone solve this? Thanks in advance
use this formula to rotate the object in corona sdk
local rotate = function(event)
if event.phase == "ended" then
myAnim1.rotation = math.ceil(math.atan2( (event.y - myAnim1.y), (event.x -myAnim1.x) ) * 180 / math.pi) + 90
end
end
i think it useful to u.........
In your release function you could add this:
myAnim1:rotate(0)
...and that will set the object to being "normal" (right side up).
You also might want to look at the code where you're doing the rotating -- you're setting the degree of rotation to the x coordinate on the screen where you're touching. If that's what you're wanting to do, you're good. It just seems weird. :)
You have a listener set that responds to all "touch" events. The problem is that "touch" events are dispatched the entire time your finger is touching the object, while you are dragging it around. If you only want to respond when you let go then you need to react to event.phase:
http://developer.anscamobile.com/reference/index/eventphase-0
So your function would look like:
local rotate = function(event)
if event.phase == "ended" then
myAnim1.rotation = event.x
end
end
OR
Another approach is to listen for "tap" instead of "touch". Personally I'd go with the solution above for future flexibility in your code, but note that while "touch" events are dispatched the entire time the finger is touching, "tap" events are only dispatched when the finger is removed. So:
myAnim1:addEventListener("tap", rotate)

Resources