How to slide pages in Corona SDK - coronasdk

I'm using CORONA SDK. I have a set of pages and I would like to slide between them using swipe left -right. The page contains a set of controls (mostly text)
What is the best way to do this slide/swipe in Corona?

Using touch events.
When the phase of event is "began" (The user just touched the screen), allow the three pages to move (the actual, the forwards and the backwards).
if event.phase == "began" then
page1.canMove = true
page2.canMove = true
page3.canMove = true
initial = {}
initial.x = event.x
end
If the pages are allowed to move:
if page1.canMove == true then
Move the three pages according to the x parameter of event.
page1.x = page1.x + event.x - initial.x
page2.x = page2.x + event.x - initial.x
page3.x = page3.x + event.x - initial.x
when the phase of event is "end" (The user release the finger), remove the permission to move.
if event.phase == "end" then
page1.canMove = false
page2.canMove = false
page3.canMove = false
end
and adjust the pages, depending on where they are.
I just invented this solution, if someone can contribute to make it more complete :D.

You can do this by using the transitions of the Storyboard
In storyboard, page is a scene, you can add transitions.
http://www.coronalabs.com/blog/2011/11/14/introducing-the-storyboard-api/

Related

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.

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.

Update the target coordinates while transitioning

I am making a game in corona and I am facing a problem. I have a circle on screen and I want it to follow the touch coordinates continuously. I am using transition.to function to do so but the thing is, whenever this function get a coordinate, it completes the transition even if the coordinates are updated during the transition.
if event.phase == "began" or event.phase == "moved" then
follow = true
touchX = event.x; touchY = event.y
elseif event.phase == "ended" then
follow = false
end
And in another function, I am doing this
if follow == true then
transition.to(circle, {time = 500, x = touchX, y = touchY, transition = easing.inOutQuad})
end
The code works fine for simple touch but I want the circle to follow the touch even when it's moving.
There are some examples which may solve your problem.
Refer:
1) Flight Path posted by carlos in corona Community.
2) Move Object through a path by renvis
Sample:
local circle = display.newCircle(10,10,20)
circle.x = 160
circle.y = 160
local olderTransition
local function moveCircle(e)
if olderTransition ~= nil then
transition.cancel( olderTransition )
end
olderTransition = transition.to(circle,{time=100,x=e.x,y=e.y})
end
Runtime:addEventListener("touch",moveCircle)
Keep Coding.......... :)
You cant add a new transition to an object, which already in a transition. Thats why you should cancel the older transition first. You can try :
local olderTransition -- This should be visible outside of your function
local function blabla()
if follow == true then
if olderTransition ~= nil then
transition.cancel( olderTransition )
end
olderTransition = transition.to(circle, {time = 500, x = touchX, y = touchY, transition = easing.inOutQuad, onComplete = function() olderTransition = nil end })
end
end
Btw If you want to drag and drop objects, transitions are bad in performance way

Corona SDK - change a variable for the duration of a drag event

I have a "seed" object which has an instance method seed:fall() which is called by my update function (which runs every frame). I've got a "touch" event listener on it so the user can drag it around. When it's being dragged, however, it's still trying to fall, which makes the drag interaction glitchy.
I've added an instance variable to my seed "class" called seed.falling. The fall() function now checks that seed.falling is true before moving the seed down the screen. The next step is to set seed.falling to false when the drag starts, and then set it back to true when the drag stops. I can't figure out this last part though.
Any ideas anyone? Is there a "stop dragging" event i could set a listener for, to switch seed.falling back on? Is there a nicer way of achieving what i want?
physics.start()
physics.setGravity(0,1)
local dd = display.newRect(400,100,200,200)
physics.addBody(dd,"dynamic")
dd:addEventListener("touch", function(event)
if event.phase == "began" then
dd.bodyType = "static"
elseif event.phase == "moved" then
dd.x,dd.y = event.x,event.y
elseif event.phase == "ended" then
dd.bodyType = "dynamic"
end
end)
I think this case is what you want?
Just for the record, here's how i solved this.
Basically i have an attribute "seed.falling" which the seed:fall method checks before moving the seed. And i set that attribute to false if we're not at the "ended" phase of the drag event, which stops the seed falling.
function Seed:new(x,y)
print("Seed:new, x = " .. (x or nil) .. ", y = " .. (y or nil) )
local seed = display.newImage("seed_icon.png")
seed.x = x
seed.y = y
seed.name = 'seed'
seed.falling = true
function seed:fall()
if(self.falling) then
self.y = self.y + 1
end
end
function seed:drag(event)
seed.x = event.x
seed.y = event.y
if(event.phase == "ended") then
seed.falling = true
else
seed.falling = false
end
end
seed:addEventListener("touch", drag)
return seed
end
function drag(event)
seed = event.target
seed:drag(event)
end
It's not a very good solution i think as it leaves the seed stranded on the screen sometimes - possibly when you drag a seed over another falling seed.

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