Cocos2D iOS tour: How to Write a hamster game (7): pop-up hamster

Source: Internet
Author: User

Cocos2D iOS tour: How to Write a hamster game (7): pop-up hamster


Pop-up hamster

Now we have confirmed that the hamster is placed in the correct position. Let's add the Code for their pop-up holes.

First, change the original z sequence of 999 to 0, so that the hamster stays under the ground.

Then add the following code to the bottom of the init method:

[self schedule:@selector(tryPopMoles:) interval:0.5];

If you have never seen it before, you can run the scheduling method on the node to tell Cocos2D to call other methods every few seconds. In this example, I want the hamster to pop up holes every 1/2 seconds.

Next, add the tryPopMoles method:

- (void)tryPopMoles:(ccTime)dt {    for (CCSprite *mole in moles) {                    if (arc4random() % 3 == 0) {            if (mole.numberOfRunningActions == 0) {                [self popMole:mole];            }        }    }     }

This method is called every 1/2 seconds. Every time, it traverses all the hamsters and gives them a 1/3 chance to pop up the cave. but only when they are not moving-a simple check is to check whether the number of currently running actions is 0.

Finally, add the implementation code of the popMole method:

- (void) popMole:(CCSprite *)mole {              CCMoveBy *moveUp = [CCMoveBy actionWithDuration:0.2 position:ccp(0, mole.contentSize.height)]; // 1    CCEaseInOut *easeMoveUp = [CCEaseInOut actionWithAction:moveUp rate:3.0]; // 2    CCAction *easeMoveDown = [easeMoveUp reverse]; // 3    CCDelayTime *delay = [CCDelayTime actionWithDuration:0.5]; // 4    [mole runAction:[CCSequence actions:easeMoveUp, delay, easeMoveDown, nil]]; // 5}

This Code uses Cocos2D actions to make the hamster pop up its cave. Pause for half a second, and then scale back. Let's explain it for a while:

Create an action to remove the hamster from the Y axis at a certain height. Because the hamster is in the cave, we will see the pop-up hamster. to make the movement look more natural. it contains a CCEaseInOut action. it will slow the mouse at the beginning and end, just like the mouse that accelerates and slows down in nature. In order to create the mouse to scale back, we only need to simply reverse the pop-up action, we will get an opposite action (only some actions can automatically generate the opposite action. cat and pig note ). the effect of creating a hamster probe to pause for 0.5 seconds is now all about the action, and we will run them in a sequence: Move up, delay and eventually move down. note that nil is used to end the sequence.

That's it! Compile and run the code, and you will see the hamster happily drill out of their caves!

What's next?

All project codes so far: sample project

Next, we will add some interesting animations: When the hamster laughs and gets K, we will increase the game logic so that you can get some scores and sound effects each time you knock the hamster.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.