Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to rotate my sprite smoothly at a constant speed. But it decreases the speed
    primarykey
    data
    text
    <p>(I'm using cocos2d for iphone) </p> <p>First I'll explain you my code: (the important parts) </p> <pre><code> -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch * touch = [touches anyObject]; CGPoint location = [touch locationInView:[touch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; float dstAngle = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, plane.position))); plane.dstAngle = dstAngle; } -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch * touch = [touches anyObject]; CGPoint location = [touch locationInView:[touch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; float dstAngle = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, plane.position))); plane.dstAngle = dstAngle; } </code></pre> <p>If I touch the screen I calculate the angle between my plane and the touch location.</p> <p>Now I set the "destination" angle of the plane. My "plane" is a subclass.</p> <p>In my header file I have these float values:</p> <pre><code>//this is in my plane subclass float diffAngle_; float startAngle_; float dstAngle_; float smoothRotationSpeed_; </code></pre> <p>EDIT: In my init of the layer I set the "smoothRotationSpeed" to a value. The "rotationTick" method IS scheduled. I have a tick method to rotate the plane (also in the plane subclass)</p> <pre><code>-(void)rotateTick:(ccTime)dt { startAngle_ = [self rotation]; if (startAngle_ &gt; 0) startAngle_ = fmodf(startAngle_, 360.0f); else startAngle_ = fmodf(startAngle_, -360.0f); diffAngle_ =dstAngle_ - startAngle_; if (diffAngle_ &gt; 180) diffAngle_ -= 360; if (diffAngle_ &lt; -180) diffAngle_ += 360; [self setRotation: startAngle_ + diffAngle_ * dt]; //smooth rotate } -(void)setSmoothRotationSpeed:(float)smoothRotationSpeed { [self schedule:@selector(rotateTick:)]; //this will be called if the "smoothRotationSpeed" value is changed } </code></pre> <p>The problem is now if I touch a location the sprites rotates to it correctly but at first it rotates a little bit fast and than it looses it's speed..</p> <p>The nearer it is to the destination angle the slower it moves...</p> <p>But I want it to move at a constant speed. This "constant" speed is defined in the "smoothRotationSpeed" value but how can I implement it in my code?</p> <p>If I set the interval to the "smoothRotationSpeed" value it wouldn't be smooth. It would lag.</p> <p>Would anyone like to help me please?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload