Corona SDK arrow to show direction of ball? - lua

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)

Related

Object keeps accelerating as it falls

I am a noob to this but anyway. I am writing an app in which you must tap in order to get an object up but at some point no matter how fast you tap the object keeps going down. it's like the gravity is getting stronger and stronger.
EDIT: I don't know what code samples to give because i don't know if any of it makes sense, but i will try:
this is how i created the object:
super=display.newImage("mine.png")
super:setReferencePoint(display.BottomLeftReferencePoint)
super.y=display.contentHeight+70
super.x=display.contentWidth/2.3
super.gravityscale=1
superIntro = transition.to(super,{time=2000, y=display.contentHeight/1.2, onComplete= supergo})
physics.addBody(super, "dynamic", {density=0, bounce=0, friction=0, radius=12})`
And this is a part of my script that i think it is relevant: `
function scrollCity(self,event)
print("touch")
if self.y > 930 then
self.y = 0
else
self.y=self.y+scrollspeed*6
end
end
function activateJets(self,event)
self.y=self.y-scrollspeed*6
print("run")
end
function touchScreen(event)
print("p")
if event.phase == "began" then
if super.y<display.contentHeight/1.2+6 then
super.y=display.contentHeight/1.2
background1.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", background1)
background2.enterFrame = scrollCity
Runtime:addEventListener("enterFrame", background2)
else
super.enterFrame = activateJets
Runtime:addEventListener("enterFrame", super)
end
end
if event.phase == "ended" then
Runtime:removeEventListener("enterFrame", super)
Runtime:removeEventListener("enterFrame", background1)
Runtime:removeEventListener("enterFrame", background2)
end
end
EDIT2: The gravity is set:
physics.setGravity( 0, 1.5 )
In the beginning a few taps are enough to keep it on the screen, but after a few seconds it is impossible to maintain and it just falls. I want it to go down with the same speed not to accelerate.
The reason it keeps accelerating is because gravity applies a constant force. Anything affected by gravity undergoes acceleration, not a fixed velocity.
For constant speed motion
Cancel the effect of gravity by setting gravityscale to 0 or setting global gravity to 0 - whichever is more appropriate for you - then setting the velocity of super using super:setLinearVelocity( xVelocity, yVelocity ).
For motion with acceleration
If you prefer an accelerating gravity then the approach you are using now is fine but you might consider super:setLinearVelocity(0, 0) when the tap occurs so that the object is falling from rest rather than continuing to fall quickly.

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

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.

Corona sdk Physics objects not interact, but detect collision

I am making a game with a guy that collects things, like coins. I want to detect the collision between these two, so I can remove the coin, but I don't want the coin to interact with the character, because right now it is slowing him down slightly. It should still interact with the ground before the collision though. Thanks for your help!
function createCoin()
for i = 1, 10 do
coin = display.newCircle(0, 0, 16)
coin.x = totallength - 1000 + i * 100
coin.y = totalheight - 200
physics.addBody(coin,
{bounce = 0, friction = 1, density = 0}
)
game:insert(coin)
coin.myName = "coin"
end
end
createCoin()
local function onCollision(event)
if event.phase == "began" then
if (event.object1.myName == "coin" and
event.object2.myName == "wheel") then
event.object1:removeSelf();
end
end
end
You cannot remove objects involved in a collision during the collision handling: see Modifying Objects" at Collision event page. Use timer.performWithDelay() as documented. This should prevent your coin from interacting with player. If that doesn't work, you could create a "ghost" object that follows the coin everywhere (same size placement etc but not visible) and is added to physics as a sensor. A sensor does not cause collision dynamics but the event is fired. You would also need to do a delayed removal of coin if removal is desired.

One by one addEventListener collision

I'm working on a simple breakout game and I've problem with ball:addEventListener( "collision", removeBricks ), it works fine until the ball hits two bricks at same time, than the up/down direction (vy) switch twice, making the ball continue moving up or down.
How can do one by one addEventListener collision and disable multiple collides at once?
function removeBricks(event)
if event.other.isBrick == 1 then
vy = vy * (-1)
...
end
end
Instead of changing ball's velocity in the removeBricks function, you can just flip a flag that means "a ball has hit some bricks and should change it's direction", and then in your enterFrame handler just change the ball's speed:
local ballHasCollided = false
local function removeBricks(event)
if event.other.isBrick == 1 then
ballHasCollided = true
end
end
local function updateBallVelocity(event)
if ballHasCollided then
ballHasCollided = false
ball.vy = -ball.vy
-- ...
end
-- your game set up code somewhere
Runtime:addEventListener('enterFrame', updateBallVelocity)

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