How do you make an object...

How do you make an object rotate from side to side?

Let’s say back and forth non-stop between -45 and 45 degrees [import]uid: 42126 topic_id: 8666 reply_id: 308666[/import]

Create some functions and if / else statements :slight_smile: [import]uid: 33866 topic_id: 8666 reply_id: 31121[/import]

Well obviously that but when i try to say something like:

local someFunction( event )
if(object.rotation > 0) then
object.rotation = object.rotation + .5
end
end

Runtime:addEventListener(“enterframe”, someFunction)

How can I get it to stop at 45, and then head back in the negative direction to -45 degrees? [import]uid: 42126 topic_id: 8666 reply_id: 31122[/import]

how about something like this…

local rotationDirection = 1

local someFunction( event )
if(object.rotation > 0) then
object.rotation = object.rotation + .5 * rotationDirection
end

if object.rotation >= 45 then
rotationDirection = -1
elseif object.rotation <= -45 then
rotationDirection = 1
end

end [import]uid: 12700 topic_id: 8666 reply_id: 31146[/import]

By doing it this way, it doesnt go back in the other direction, it continues along in one direction. The program is treating -45 as 315 degrees.

Any other suggestions? [import]uid: 42126 topic_id: 8666 reply_id: 31149[/import]

Hi,

I tested out the code on an object and it seems to work. It moves 45 degrees…and then goes back to -45 degrees (315 degrees)…let me know exactly what you had in mind.

I thought you wanted something where the object rotates back and forth between -45 degrees (315 degrees) to 45 degrees.

Let me know :slight_smile: [import]uid: 12700 topic_id: 8666 reply_id: 31151[/import]

How did you write the function exactly?
Because mine doesnt go back it continues. [import]uid: 42126 topic_id: 8666 reply_id: 31152[/import]

How did you write the function exactly?
Because mine doesnt go back it continues. [import]uid: 42126 topic_id: 8666 reply_id: 31153[/import]

I got it to work.

Now lets say that I want to project an object from the end of the one that is rotating. For example, the object rotating is a cannon and i want to fire a ball from it’s end. What would I set my x and y positions of the ball? [import]uid: 42126 topic_id: 8666 reply_id: 31154[/import]

The easiest way to do it is…

put the cannon and ball in their own group and then rotate the group…

something like this…

local cannonGroup
local cannon
local ball

.
.
.
*** inside the enterFrame code ***

if(cannonGroup.rotation >= 45) then
rotationDirection = -1
elseif (cannonGroup.rotation <= -45) then
rotationDirection = 1
end

cannonGroup.rotation = buttonGroup.rotation + .5*rotationDirection

*****
**** Inside the place where you create the objects ***

cannonGroup = display.newGroup()

cannon = display.newImage(“cannon.png”)
ball = display.newImage(“ball.png”)

cannonGroup:insert(cannon)
ball.x = cannon.width - 25 – adjust as needed
ball.y = cannon.height/2 – adjust as needed
cannonGroup:insert(ball)

cannonGroup.x = 175
cannonGroup.y = 100
localGroup:insert(cannonGroup)

****
[import]uid: 12700 topic_id: 8666 reply_id: 31180[/import]

Hey mdk5316,

Thanks for creating this thread.

Could I see an example of the coding you used to rotate your cannon and fire the ball? I am having the same problem however I do not know how to setup the coding to angle the cannon by touch.

I want to be able to move the cannon to whatever desired angle I’d like by dragging it with my finger. Any help would be much appreciated! [import]uid: 46629 topic_id: 8666 reply_id: 33936[/import]

Hi. I’m eleven years old and a novice at Corona. This may be relevant to the current topic (having balls shoot in the correct direction).

I created a cannonBall and a cannon and made them a single group and put them in the same place. I created a working method of rotating the cannon via touch:

local function rotateTo (event)
local deg = math.atan2( event.y - cannonGroup.y, event.x - cannonGroup.x )
cannonGroup.rotation = math.deg(deg)
end

However, after creating a shootBall function (using the code given kindly by indiegamepod) and applying it to the rotateTo function:

local function rotateTo (event)
if ( event.phase == “began” ) then
local deg = math.atan2( event.y - cannonGroup.y, event.x - cannonGroup.x )
cannonGroup.rotation = math.deg(deg)
end
if ( event.phase == “ended” ) then
timer.performWithDelay( 1, shootBall )
end
end

the cannon will instantly face the touch, but will not continue rotating with it, and the shootBall function will not call. Why is this? [import]uid: 82408 topic_id: 8666 reply_id: 57138[/import]