i’m new to corona i i was wondering if anyone could help me. I have an object following the users finger and i want some spawned object to follow the object that is following the users finger.
thanks
Ben [import]uid: 32380 topic_id: 7444 reply_id: 307444[/import]
Look up events in the Corona api, specifically the “enterFrame” event type.
The way I’d start would be to create an “enterFrame” listener (which is called on every frame) for the object that’s supposed to be following the other object and then modify the x/y values of that object to get incrementally closer to the other object.
For example, let’s say you have object1 and object2.
object2 is supposed to be following object1.
Create an “enterFrame” listener for object2.
Within that listener function, check the x/y values of object1. Based on that information, either increase or decrease the x/y values of object2 by a small amount (e.g. x = x + 1 ).
Look up the events and learn about them from the API documentation and the examples. Feel free to reply and post more specific questions if you get stuck with that.
Hope that helps! Should get you started anyway… [import]uid: 7849 topic_id: 7444 reply_id: 26545[/import]
hey thanks Jonbeebe but i was going at having object 2 follow object 1 even if it wasn’t moving for example
If object 1 is moving slow or fast it doesn’t matter to object 2 it will still be following object 1 at the same rate the only thing that would be sort of connected between the 2 objects would be which way object 2 would follow object 1. i hope that makes sense and thanks in advance!!
[import]uid: 32380 topic_id: 7444 reply_id: 26852[/import]
Look at your events? Try something like:
local function onDropCollision(self,event)
where you can access both objects. I can not see your code, so difficult to comment. Your “finger” should be accessable at this point? [import]uid: 8576 topic_id: 7444 reply_id: 26920[/import]
so the part of the code im talking about is
local image = display.newImage( “image2.png” )
– Setup listener
local myListener = function( event )
image.x = event.x
image.y = event.y
end
image:addEventListener( “touch”, myListener )
local fish = display.newImage (“image.png”)
fish.x= 100
fish.y= 100
local fishListener = function( event )
fish.x = event.x
fish.y = event.y
end
fish:addEventListener( “touch”, fishListener )
and i want fish to follow image at a certain speed
thanks a lot in advance!!!
Ben [import]uid: 32380 topic_id: 7444 reply_id: 27281[/import]
Tried adding a transition.to function?
local fish = display.newImage ("image.png")
fish.x= 100
fish.y= 100
local fishListener = function( event )
transition.to(fish, { time=500, x=event.x, y=event.y })
end
fish:addEventListener( "touch", fishListener )
See if this works. I’m not sure if event’s can be used inside a transition.to but try it:-) [import]uid: 14018 topic_id: 7444 reply_id: 27305[/import]
Then you’d have to add the object that’s following inside an enterFrame function, to check it’s position every frame. And if it’s not in the same position as the image, create a transition.to-function to move it to the image’s coordinates.
local function followFunction()
if(fish.x ~= image.x or fish.y ~=image.y) then
local fish = display.newImage ("image.png")
fish.x= 100
fish.y= 100
local fishListener = function( event )
transition.to(fish, { time=500, x=event.x, y=event.y })
end
fish:addEventListener( "touch", fishListener )
end
end
Runtime:addEventListener("enterFrame", followFunction)
Try this and see if it works [import]uid: 14018 topic_id: 7444 reply_id: 27447[/import]
thanks [import]uid: 32380 topic_id: 7444 reply_id: 27428[/import]
i was able to get the fish to follow the image on the your fish:addEventListener( “touch”, fishListener )
i just had to switch it to a pig:addEventListener(“touch”,fishListener) but its glitchy like its trying to decide on where to go but i will check out this new think you have up thank you so much! [import]uid: 32380 topic_id: 7444 reply_id: 27449[/import]
So i have been messing around with you code but i can’t figure out how to spawn more fish’s and have them follow the image. i got the fish to spawn by adding
timer.performWithDelay(1000, followFunction, 9)
timer.performWithDelay(1000, fishListener, 9)
thanks
[import]uid: 32380 topic_id: 7444 reply_id: 27452[/import]