简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM