Corona SDK Animate a newImageRect - coronasdk

I am wanting a game object with both physics as well as animation sequences. I want to animate my character with a sprite sheet with predefined sequences while taking advantage of gravity, velocity, and other physics effects.
Is it possible to to add animation sequences to a newImageRect. Or add physics to a newSprite? Or join a sprite to a rectangle?

To create an animation in Corona SDK, it's best to use this resource
You can connect the animation using this code
display.setStatusBar( display.HiddenStatusBar )
-- lua data file that Texture packer published
-- it contains the data required by newImageSheet
-- and also contains a function to retrieve frames
-- by their names sheetInfo:getFrameIndex("sprite")
local sheetInfo = require("spritesheet")
-- init the image sheet
local myImageSheet = graphics.newImageSheet( "spritesheet.png",
sheetInfo:getSheet() )
local sequenceData = {
-- set up anmiation
{
name="walk", -- name of the animation (used with setSequence)
sheet=myImageSheet, -- the image sheet
start=sheetInfo:getFrameIndex("capguy/walk/0001"), -- name of the first frame
count=8, -- number of frames
time=1000, -- speed
loopCount=0 -- repeat
},
}
capguy = display.newSprite( myImageSheet, sequenceData )
-- set initial position and direction
capguy.x = 160
capguy.y = 300
direction = 1
-- start walking animation
capguy:setSequence("walk")
capguy:play()
For physics - Physics Editor

I figured out that the sprite sheet wasn't acting the way that I wanted it to because it wasn't added to the world. This line fixed the problem. world:insert(capguy)

Related

How to change the velocity in each frame in Love2D?

I'm trying to code a Pong game on Lua using the framework Love2D with some extra features. Among others, I want curves to occur. In order to do so, I'm trying to implement the trajectory of horizontally launched projectiles. I have a ball table with a position attribute for x (ball.x) and another for y (ball.y). I, also, have a an attribute for the x velocity (ball.dx) and another for the y velocity (ball.dy). Finally, I have an acceleration variable (gravity)
In my game, if the paddle moves and the ball hits it, the ball should follow and horizontal curve. In order to create my curves, I want to change the y-axis velocity on each frame in order to make my ball move in an arc across the screen. The main issue that I have is that I don't know how to change this velocity in each frame in order to create the expected arc. The most recent attempt that I made was to create a while loop like the following code. However, it creates an infinite loop. Can someone enlighten me please?
Clarification:
-Player.x and player.y are the player coordinate
-Player2.x and Player2.y are the opponent coordinate
-This piece of code is inside another if statement detecting a collision. It is inside the love.update(dt) function.
Thank you so much!
if love.keyboard.isDown("up") and distanceBetween(ball.x,ball.y,player.x,player.y)>30 then
ball.dy=0
while CollisionDetector(ball.x,player2.x,ball.y,player2.y, player2.width,player2.height,ball.size)==false or ball.x>0 or ball.y-20>0 or ball.y+20<love.graphics.getHeight() do
ball.dy=ball.dy+gravity*dt
ball.y=ball.y+ball.dy
end
end
I believe the snippet you've provided is a part of love.update function. (At least it should be.) That function is supposed to perform a single step of the game at a time and return. It is not supposed to handle the whole flight in a single call.
In your particular case, while loop expects the ball to move, but that is not something that will happen until the loop and encompassing function end.
In general, to simulate ongoing processes you'll have to store the information about such process. For example, if you want gravity be dependent on the paddle movement direction, then you'll have to modify the corresponding variable and store it between the call.
In your code, there are multiple other design flaws that make it do not what you think it should do. With some functions and code parts left to be implemented by yourself, the code outline would look as follows:
function collides(ball,player)
return (ball.x,player.x,ball.y,player.y, player.width,player.height,ball.size)
end
function love.update(dt)
handle_paddle_movements()
--handle the ball-paddle interaction
if collides(ball,player1) or collides(ball,player2) then
ball.dx=-ball.dx
if love.keyboard.isDown("up") then
--store the info you need to know about the interaction
ball.d2y = -gravity
else if love.keyboard.isDown("down")
ball.d2y = gravity
else
ball.d2y = 0
end
end
handle_the_wall_collision()
--ball movement code should be separate from collision handling
--concrete equations can be whatever you want
ball.x = ball.x+ball.dx*dt
ball.dy = ball.dy + ball.d2y * dt
ball.y = ball.y + ball.dy * dt
end

Moving multiple objects a fixed distance

I am currently making a game where I have several platforms flying around the screen. I have grouped all of them using the display.newGroup() and inserting them into that group. I would like to move these multiple platforms (which are all the same) a fixed distance at the same time. How would I do that?
I have tried using translation, but then, it seems that I can not move all of them a 'fixed' distance using it. Is there any other option?
Use transition instead of translation, here s an example
local group = display.newGroup()
group.x = value
group.y = value
-- (1) move square to bottom right corner; subtract half side-length
transition.to( group , { time=1500, alpha=0, x=(w-50), y=(h-50), onComplete=listener1 } )
For more details,
https://docs.coronalabs.com/api/library/transition/to.html#examples

How to move a few objects together to the left and still collide with ball?

I got a few tower like things that a ball can jump on them.
The problem is that I can't make the towers move like +5 to left and still be collided.
Can someone help me?
CODE
_G.mainGroup = display.newGroup()
local rect=display.newRect( 200, 400, 100, 100 )
physics.addBody(rect, "static", {density= 3,friction= 0.2})
mainGroup:insert(rect)
-- timer.performWithDelay(1, function(e)
-- mainGroup.x = mainGroup.x - 4
-- end, 0 )
I tried this in my code. and the rectangle moves without collison with a ball but it leaves behind a invisible replica which has collision becoz a ball is able to bounce on it.
You should use kinematic objects instead of static objects. Static objects are used for objects which should stay stable for a long time. Kinematic objects can be stable in small times.

Changing a moving objects direction of travel in corona

I'm new to Corona and looking for a little help manipulating moving objects:
Basically I want a set up where when I can click on a moving object, a dialog box will pop up giving me the option to change the speed of the object and the vector of travel. I'm pretty sure I can figure out the event handling and the dialog but I'm stuck on simply changing the direction of travel to the new vector
in a simple example, I have a rect moving up the screen as follows:
obj1 = display.newRect(500, 800, 10, 40)
transition.to(obj1,{x=500, y = 100, time = 40000})
I know I can change the speed by adjusting the time, but if I use
obj1:rotate(30)
to turn the object 30 degrees, how do I make it travel in the new direction?
Should I be using physics - linear impulse for example, instead of transitions?
Apologies if this is a stupid question but I have searched without success for a solution.
This sounds like what you are trying to do. You would have to modify bits to fit your code but this is a working example. So if you copy it to a new main.lua file and run it you can see how it works. (Click to rotate obj).
local obj = display.newRect(50,50, 10, 40)
local SPEED = 1
local function move(event)
obj.x = obj.x + math.cos(math.rad(obj.rotation)) * SPEED
obj.y = obj.y + math.sin(math.rad(obj.rotation)) * SPEED
end
local function rotate(event)
obj.rotation = obj.rotation + 45
end
Runtime:addEventListener("enterFrame", move)
Runtime:addEventListener("tap", rotate)
Basically I used the "enterFrame" event to 'move' the rectangle, by recalculating the x and y of the objects location using its rotation (which is easy enough to modify) every frame.

corona drag and drop an object on container or reference

I am developing a game and one level is solving a jumbled up word. The letters are images like tiles and I am trying to either click a letter and this will move to the first of the allocated spots to solve the word or to actually drag the tile to the spot. Can anyone help me implement this?
To drag an object see this tutorial:
http://www.coronalabs.com/blog/2011/09/24/tutorial-how-to-drag-objects/
If you just want to tap them and have them move to where you want them, then something like this might work for you:
local function moveTile(event)
-- put your code here to move the tile
-- figure out the X, Y where the tile needs to move to
-- either just set the X, Y on the tile or use a transition.to() call
-- to animate it.
local thisTile = event.target
thisTile.x = destinationX -- you have to figure out what destinationX is.
thisTile.y = destinationY -- figure this out too
return true
end
Then on each tile after you create it, simply add this line to make it tappable:
tile:addEventListener("tap", moveTile)

Resources