Ball rotation?

How can i applay a costant rotation to  display objescts??

You can do this with physics or by using a image sheet to animate your display objects. For animating from image sheet check this out. It helped me a lot.

http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

I don’t have a link for doing this with physics because i have not really explored that part of corona but i imagine you can find something with a google search.

There are two ways without physics and one with.  

  1.  Use an enterFrame event listener on the runtime that increments the object’s .rotation parameter a little bit on each frame.

  2.  Use a transition.to() API call to rotate the object over 360 degrees of some period of time, and with it’s onComplete parameter, have it do it again.  Transitions 2.0 will make this time of thing much easier than it is with Transitions 1.0, but you won’t get T2 until the next public build.

With physics you can add some rotational force to the object, but you will want to potentially have that in a Runtime enterFrame loop as well to keep it going.

Why did the rect not rotate??

  local rect = display.newRect(100, 200, 300, 20)     local function rotate(event) rect.rotation = 180 end   Runtime:addEventListener( "enterFrame", rotate )  

It rotated. But rectangle upside down looks the same :stuck_out_tongue:

Let’s assume that your “rect” will be arrow pointing up.

What you wrote is: “on each ‘enterFrame’ make arrow pointing down”

:slight_smile: …And how can i applay an infinite rotation to the rect?

Your rectangle rotated in the enterframe function, you just rotated it to 180 degrees in every frame.

I think what you want is to smoothly rotate constantly - setting .rotation to 180 immediately rotates the shape to 180 degrees, not smoothly. For that you need a transition.

The simplest way to rotate an object continuously is with a repeating transition (not as scary as it sounds)…

local rect = display.newRect( 200, 200, 100, 100 ) function rotateRect() rect.rotation = 0 transition.to( rect, { time=5000, rotation=360, onComplete=rotateRect } ) end rotateRect()

If you want to rotate a physics object use the code I posted here:

http://forums.coronalabs.com/topic/39690-rotate-a-physics-object/?p=206113

In my app there is a ball when i schoot it, the ball begans to rotate, but when the ball falls back to the ground, the rotation ends immediately and the ball continues to move  without rotation!!

What can i do to avoid that the ball’s rotation ends before that the ball is stopped??

Post your code.

  module(..., package.seeall)         local W = display.contentWidth   local H = display.contentHeight    function new() local localGroup = display.newGroup()     local localGroup = display.newGroup()   local pausa = display.newImage("pausa.png") pausa.x = 600 pausa.y = 40   local bglivelli = display.newImage("grass.jpg") bglivelli.x = W/2 bglivelli.y = H/2     local levelText = display.newText("level 1", 45, 1200, "Arial", 50)     local ground1 = display.newImage("linea.jpg") ground1.x = W/2 ground1.y = 1100 physics.addBody(ground1,"static",{})   local circle = display.newImage("palla.png", 80, 80) circle.x = 367 circle.y = 870      physics.addBody(circle,"dinamic", {density = 1.0, friction = 0.3, bounce = 0.2, radius = 35})     local ground2 = display.newRect(0, 1280, 720, 1) physics.addBody(ground2, "static", {})     local roof1 = display.newRect(0, 0, 180, 0.5) physics.addBody(roof1, "static", {})    local roof2 = display.newRect(540, 0, 180, 0.5) physics.addBody(roof2, "static", {})            local wallS = display.newRect(0,0,0.5, 1280) physics.addBody(wallS, "static", {})   local wallD = display.newRect(720, 0, 1, 1280) physics.addBody(wallD, "static", {})   local bar1 = display.newImage("bar.png") bar1.x = 180 bar1.y = 50 physics.addBody(bar1, "static", {})   local bar2 = display.newImage("bar.png") bar2.x = 540 bar2.y = 50 physics.addBody(bar2, "static", {})   local reloadB = display.newImage("reloadB.png") reloadB.x = 120 reloadB.y = 40 reloadB.scene = "level1"     local sX = bar1.x local sY = bar1.y   local eX = bar2.x local eY = bar2.y            localGroup:insert(roof1)  localGroup:insert(roof2)  localGroup:insert(bglivelli)  localGroup:insert(ground1)  localGroup:insert(levelText)  localGroup:insert(circle)  localGroup:insert(ground2)  localGroup:insert(wallS)  localGroup:insert(wallD)  localGroup:insert(bar1)  localGroup:insert(bar2)  localGroup:insert(reloadB)  localGroup:insert(pausa)      function changeScene(e) if e.phase == "ended" then director:changeScene(e.target.scene) end end    local physics=require ("physics") local isPaused = false   function pausePhysics( event )     if "began" == event.phase then         if isPaused == false then             physics.pause()             isPaused = true                                       local mainB = display.newImage("button1.png")             mainB.x = W/2             mainB.y = H/2             mainB.scene = "menu"             mainB:addEventListener("touch", changeScene)                          local mainBText = display.newText("Main Menu", 0, 0, "Arial", 50)             mainBText.x = mainB.x             mainBText.y = mainB.y                          local levelSelB = display.newImage("button1.png")             levelSelB.x = mainB.x              levelSelB.y = mainB.y - 150             levelSelB.scene = "levelSel"             levelSelB:addEventListener("touch", changeScene)                          local levelSelBText = display.newText ("Level Select", 0, 0, "Arial", 40)             levelSelBText.x = levelSelB.x             levelSelBText.y = levelSelB.y                          local resumeB = display.newImage("button1.png")             resumeB.x = mainB.x              resumeB.y = mainB.y + 150                          local resumeBText = display.newText("Continue", 0, 0, "Arial", 50)             resumeBText.x = resumeB.x             resumeBText.y = resumeB.y                          function resume(e) if "began" == event.phase then if isPaused == true then  physics.start()  isPaused = false    --remove buttons  mainB:removeSelf()  mainBText:removeSelf()  levelSelB:removeSelf()  levelSelBText:removeSelf()  resumeB:removeSelf()  resumeBText:removeSelf()  end  end  end  resumeB:addEventListener("touch", resume)                                                                                                                    -- elseif isPaused == true then            -- physics.start()             --isPaused = false          end     end end       pausa:addEventListener( "touch", pausePhysics )   physics.start()    function circleTouch(event) if event.phase == "began" then display.getCurrentStage():setFocus(circle)   else if  event.phase == "ended" then   circle:removeEventListener("touch", circleTouch)   circle:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y, circle.x, circle.y) display.getCurrentStage():setFocus(nil)   function rotateCircle()     circle.rotation = 0     transition.to( circle, { time=5000, rotation=360, onComplete=rotateRect } ) end rotateCircle()       end end end     circle:addEventListener("touch", circleTouch)                 local function checkForHit(object, from\_x, from\_y, to\_x, to\_y)  local hits = physics.rayCast( from\_x, from\_y, to\_x, to\_y )  if hits[1].object == circle then print ("You Win") local text = display.newImage( "button1.png") text.x = W/2 text.y = H/2 localGroup:insert(text) text.scene= "level1" text:addEventListener("touch", changeScene)   local textT = display.newText("Next Level", 0, 0, "Arial", 45) textT.x = text.x textT.y = text.y     end end         Runtime:addEventListener( "enterFrame", function() checkForHit(circle, sX, sY, eX, eY )end  )   reloadB:addEventListener("touch", changeScene)             return localGroup   end    

I can’t run that code because it isn’t complete - you’ve not posted enough code for me to run.

The first rule of debugging is Boil your code down to just the problem code. That way, you can work on the part which is a problem without interfering with other code and, if you need to, you can post it for other people to debug.

Reduce your code to just the piece which is the problem, then post it.

If I understand your description properly, the ball (with enough friction) would stop spinning when it hits another object, but would bounce off somewhere else. It all depends on your specific body setup.

And what can i do to solve the problem?

Well, I did just tell you to reduce the code down to the only code which has the problem. Until then, I can’t help you!

You can do this with physics or by using a image sheet to animate your display objects. For animating from image sheet check this out. It helped me a lot.

http://www.coronalabs.com/blog/2012/10/02/animated-sprites-and-methods/

I don’t have a link for doing this with physics because i have not really explored that part of corona but i imagine you can find something with a google search.

There are two ways without physics and one with.  

  1.  Use an enterFrame event listener on the runtime that increments the object’s .rotation parameter a little bit on each frame.

  2.  Use a transition.to() API call to rotate the object over 360 degrees of some period of time, and with it’s onComplete parameter, have it do it again.  Transitions 2.0 will make this time of thing much easier than it is with Transitions 1.0, but you won’t get T2 until the next public build.

With physics you can add some rotational force to the object, but you will want to potentially have that in a Runtime enterFrame loop as well to keep it going.

Why did the rect not rotate??

  local rect = display.newRect(100, 200, 300, 20)     local function rotate(event) rect.rotation = 180 end   Runtime:addEventListener( "enterFrame", rotate )  

It rotated. But rectangle upside down looks the same :stuck_out_tongue:

Let’s assume that your “rect” will be arrow pointing up.

What you wrote is: “on each ‘enterFrame’ make arrow pointing down”

:slight_smile: …And how can i applay an infinite rotation to the rect?

Your rectangle rotated in the enterframe function, you just rotated it to 180 degrees in every frame.

I think what you want is to smoothly rotate constantly - setting .rotation to 180 immediately rotates the shape to 180 degrees, not smoothly. For that you need a transition.

The simplest way to rotate an object continuously is with a repeating transition (not as scary as it sounds)…

local rect = display.newRect( 200, 200, 100, 100 ) function rotateRect() rect.rotation = 0 transition.to( rect, { time=5000, rotation=360, onComplete=rotateRect } ) end rotateRect()