Move Objects

Im new in Corona, im trying to move birds (y-cordination), from let to right of the screen (one after the other).
The problem is i don’t know how to set different listeners for different objects; currently, within the provided code, once the second object is shown, the first object stops and the second keeps moving.

Im trying to show birds every one second using the Movie Clip library and then I want to move every bird individually from left to right without stopping the other birds till the bird disappears (left the screen from the right side)
local flyingBird
local dx, dy, grav = {},{},{}
local birdsCounter = 0
local physics = require “physics”
local movieclip = require(“movieclip”)
require “sprite”

display.setStatusBar (display.HiddenStatusBar)
local localGroup = display.newGroup()
local objectBackground = display.newImage(“images/background.png”)
localGroup:insert(objectBackground)

function createBirdObject()
birdsCounter = birdsCounter + 1
flyingBird = movieclip.newAnim(12,{“images/bird/1.png”, “images/bird/2.png”,“images/bird/3.png”})
local random = math.random
flyingBird.x = 100
flyingBird.y = 75
flyingBird.xScale = .4
flyingBird.yScale = .4
flyingBird:play()
dx[birdsCounter] = 2
dy[birdsCounter] = 3
grav[birdsCounter] = 0.0
localGroup:insert( flyingBird )
Runtime:addEventListener( “enterFrame”, moveSprite )
end

– Function to move every bird around screen
function moveSprite( event )
local bird = event.target
bird.x = bird.x + dx[birdsCounter]
if ((bird.x < 30) or (bird.x > 80)) then
dy[birdsCounter] = - dy[birdsCounter] - grav[birdsCounter] – i used [birdsCounter] this to set the default parameters for each new object
end
dy[birdsCounter] = dy[birdsCounter] + grav[birdsCounter]
end

local createNewBirdEveryoneSecond = timer.performWithDelay( 2000, createBirdObject, 2 )
[import]uid: 11038 topic_id: 3774 reply_id: 303774[/import]

there is no reason to have different arrays for dx, dy, grav etc you can add them to your bird object

also add your birds to an overall array

[lua]local birds = {}

function createBirdObject()

local flyingBird = display.newImage(“bird.png”)

flyingBird.dx = 2
flyingBird.dy = 3
flyingBird.grav = 0.0

flyingBird.x = 0
flyingBird.y = 100

– this is basically a “push”, if length of array is #birds
– then the new element will be at #birds+1
birds[#birds+1] = flyingBird

end

function moveSprite(event)

local newX, bird

– loop through the array backwards so that
– if any birds are removed it doesnt affect the length of the
– array incorrectly as you loop through it

for b=#birds, 1, -1 do

bird = birds[b]

newX = bird.x + bird.dx

if(newX > 300) then

bird:removeSelf()
table.remove(birds, b)
print(birds, #birds)

else

bird.x = newX

end

end

end

Runtime:addEventListener( “enterFrame”, moveSprite )

local createNewBirdEveryoneSecond = timer.performWithDelay( 1000, createBirdObject, 8 )[/lua]

[import]uid: 6645 topic_id: 3774 reply_id: 11453[/import]

Thank you very much.

I really need you to help me in one thing that im really dying to understand. 1st, how did you define the DX,DY,GRAV as properties for the flyingBird object. they even are just parameters. I know you may say this is a silly question.

[import]uid: 11038 topic_id: 3774 reply_id: 11470[/import]

that’s it just try it. you can add what you want, they’re all just dynamic tables anyway [import]uid: 6645 topic_id: 3774 reply_id: 11478[/import]

Thanks again
I have a shotgun object displayed on the screen, I was able to apply the setDrag feture using the movieclip. I would like to move this object around it self via touch/button to acconadate the sniping method. I’m wondering how to mice this object around it self. In addition, I want to apply a bullet ti be fired once the touch is applied to through a bullet to the desired bird/object

I’m wondering if this is doable via corona APIs

[import]uid: 11038 topic_id: 3774 reply_id: 11493[/import]

I’m facing problems on moving the gun to point to the moving birds. I’m wondering how this could be done [import]uid: 11038 topic_id: 3774 reply_id: 11494[/import]

you’ll have to explain better sorry. can you illustrate the problem with a sketch. stick it on tinypic.com and add here with

![](http://www.tinypic.com/......) [import]uid: 6645 topic_id: 3774 reply_id: 11497[/import]

Shotgun

Sorry for disturbance!

What I’m trying to do here is i’m displaying a shotgun on the screen. I want to move the shotgun around it self; meant by that if the user tries to drag the shotgun up it will move the it up to point to the bird to shot without moving the shotgun it self up. in addition, once the user clicks on fire (assuming a fire button is there), a bullet will be fired to shot the flying bird.

Thanks
[import]uid: 11038 topic_id: 3774 reply_id: 11501[/import]

i recognize those birds :wink: http://www.spritely.net/

[import]uid: 6645 topic_id: 3774 reply_id: 11511[/import]

I didn’t get you point? The birds here is just an example.

Any proposed solution for what i have asked? [import]uid: 11038 topic_id: 3774 reply_id: 11516[/import]

it doesnt really show how you intend to move the gun. it wouldnt rotate. but presumably you have a sprites of it tipping backwards and also switching sides from left to right? will it’s y position change or are you keeping it at the bottom?

are you going to calculate a 3D trajectory of the bullet to birds in the distance?
[import]uid: 6645 topic_id: 3774 reply_id: 11517[/import]

Yes i’m intending to keep the shotgun at the button with the ability to move the shotgun barrel to up as well. In addition I’m planning to calculate the 3D trajectory of the bullet to birds in the distance?

Thanks jmp909 [import]uid: 11038 topic_id: 3774 reply_id: 11521[/import]

i’m not sure what you’re having a problem with exactly. because it depends how you want it to work. you can drag the gun like this
http://developer.anscamobile.com/content/drag-me

then determine how far away it is from the centre of the screen to decide which animation sprite to show. but for instance how will you shoot birds on the far left… if you drag the gun to the left, and it points to the right. then you can’t shoot the bird on the left. what is your gameplay?

maybe you should drag your crosshair to the bird and click to fire. you can do this twice. then click the gun to reload. then repeat dragging the crosshair again?

or maybe you can move the gun left right by dragging the bottom of the gun, and move the tip of the gun left/right by dragging the tip of the barrel.

Without knowing your gamplay mechanism I don’t know what to suggest. [import]uid: 6645 topic_id: 3774 reply_id: 11528[/import]

Thanks for your quick response.

What I’m trying to do is moving the gun left right by dragging the bottom of the gun, with the ability to move the tip of the gun left/right by dragging the tip of the barrel. In the same time while moving the gun, the barrel will be moved and pointing to birds flying on the screen. Thereafter, the fire button would be pressed to down the targeted bird
[import]uid: 11038 topic_id: 3774 reply_id: 11542[/import]

ok now that’s clear… what is the problem you are having? :slight_smile: [import]uid: 6645 topic_id: 3774 reply_id: 11550[/import]

how to control the shotgun, i mean to move it as i have described with the crosshair icon for targeting objects .
Shotgun [import]uid: 11038 topic_id: 3774 reply_id: 11553[/import]

well first you could use these


event.x
is the x-position in screen coordinates of the touch.
event.y
is the y-position in screen coordinates of the touch.
event.xStart
is the x-position of the touch from the “began” phase of the touch sequence.
event.yStart
is the y-position of the touch from the “began” phase of the touch sequence

actually i would probably use some invisible objects (red & green below) that covered the top and bottom parts of the gun to work out which was touched. but you will have to move these relative to your graphic as it moves. the maths for this really depends on the size of your gun graphic though and how it animates.

[import]uid: 6645 topic_id: 3774 reply_id: 11554[/import]

Yes but how could i move the object (the barrel part) up without moving the gun ti self. One more thing, how to draw the crosshair and to be joint to the gun (lets say 100px far from the gun) so it can move the gun and the crosshair in parallel.

Thanks [import]uid: 11038 topic_id: 3774 reply_id: 11565[/import]

surely you have animation states for the gun at different angles? [import]uid: 6645 topic_id: 3774 reply_id: 11567[/import]

You mean you want transform controls to distort the image? Because that seems like the only way to move the gun without moving the gun.

You have to draw the images for the gun in different positions, scale/rotate it, or use separated graphics that won’t look odd when moving independently. [import]uid: 11024 topic_id: 3774 reply_id: 11569[/import]