I am new to corona and I am trying to make multiple objects get moving, and selectively make them rotate one at a time. I should be able to select any one object by tapping on it, and then by clicking a button, it should rotate by X degree (for each click). I have the following code working for one object. But stuck on getting the setup for multiple moving objects. I apologize if it is basic.
local Button = display.newRect(200,200, 10, 40)
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)
Button:addEventListener("tap", rotate)
Just update your code to have a "selected" object, like this:
local selectedObject = nil
local Button = display.newRect(200,200, 10, 40)
local obj = display.newRect(50,50, 10, 40)
local SPEED = 1
local function select(event)
selectedObject = event.target
end
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)
if selectedObject then
selectedObject.rotation = selectedObject.rotation + 45
end
end
obj:addEventListener("tap", select)
Runtime:addEventListener("enterFrame", move)
Button:addEventListener("tap", rotate)
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.