Animate Sprite Vertically

Discussion in 'iOS Programming' started by dannybrammy, Mar 16, 2014.

  1. dannybrammy macrumors newbie

    Joined:
    Mar 16, 2014
    #1
    I am having real trouble getting a sprite to spawn on the screen at the top and then animate it from the top to the bottom. I have been following Ray Wenderlich’s tutorial on creating a simple game however that moves the sprite from right to left, I would like it to move from top to bottom and now I’m tremendously stuck! Below is my current code:

    Code:
    - (void)addComet:(CCTime)dt
    {
        // Make sprite
        CCSprite *comet = [CCSprite spriteWithImageNamed:@"PlayerSprite.png"];
        
        // Verticle spawn range
        int minY = comet.contentSize.width;
        int maxY = self.contentSize.height - comet.contentSize.height / 2;
        int rangeY = maxY - minY;
        int randomY = (arc4random() % rangeY) + minY;
        
        // Position comets slightly off the screen
        comet.position = CGPointMake(self.contentSize.width + comet.contentSize.width, randomY);
        [self addChild:comet];
        
        // Duration range comets take to fly across screen
        int minDuration = 2.0;
        int maxDuration = 4.0;
        int rangeDuration = maxDuration - minDuration;
        int randomDuration = (arc4random() % rangeDuration) + minDuration;
        
        // Give comet animation
        CCAction *actionMove = [CCActionMoveTo actionWithDuration:randomDuration position:CGPointMake(0, 500)];
        CCAction *actionRemove = [CCActionRemove action];
        [comet runAction:[CCActionSequence actionWithArray:@[actionMove,actionRemove]]];
    }
    If anyone could point me in the right direction because I’ve been stuck on this for quite a while and just cannot get the sprite to spawn randomly at the top of the screen and then animate down to the bottom. I have also looked at sample code such as tweejump but have had no luck.
     
  2. DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #2
    I'm not sure if this would be any use for your scenario, but for me it works fine.
    I fire an NSTimer for the gameplay. Throughout the timer i am resetting the object's center points continously. And set an 'gameSpeed'. This is from an vertical running scroller

    Code:
    -(void)gamePlayTimer {
      switch(gameState) {
        //....
        case 1:
         //Loads of stuff, player collision check etc.
         player.center = CGPointMake(player.center.x, player.center.y + gameSpeed);
        break;
        //...
      }
    }
    
     
  3. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #3
    Couple of comments:
    1. mixing Y (height concept) with .width
    2. moving to a fixed point (0, 500)

    Hope this helps :)
     

Share This Page