(No. 00004) implementing the brick-hitting game for iOS (12): scalable, I am a golden hoop (on )!

Source: Internet
Author: User

(No. 00004) implementing the brick-hitting game for iOS (12): scalable, I am a golden hoop (on )!

The original or translated works of the giant panda and pig hope. You are welcome to reprint them. Please indicate the source for reprinting.
If you think it is not easy to write, please let me know. If you think it is good, please give me a thumbs up. Thank you! Hopy ;)

The general star category has been completed. Next we will implement the specific variable length and shortening item.

Variable-length rebound Rod

We want to implement the following function: If the bounce rod is received after the falling star changes to a long star, the length of the bounce rod will be changed to twice the original length.

It seems simple, but there is actually a problem. you can't just extend the length of the sprite texture of the bounce stick, because you are using a physical engine in this game, the size of the physical object of the bounce stick cannot be changed at will during game operation.

Therefore, we need to find a way to extend the physical size of the rebound rod. Of course, we also need to extend the size of the sprite frame so that they can work together to achieve a realistic extension effect.

Here, the cat uses the method of stealing beams and columns, uses Ai to create an extended rebound rod, and adjusts its physical object to adapt to the new length. Then, when the rebound rod needs to grow, replace the old one with the new one.

First, use Ai to create a long bounce stick. then create a StickLonger. ccb file in SpriteBuilder and set the boundary of its physical object:

Implement the variable star feature

Return to Xcode and add a new property to the interface file of the Stick class:

+(instancetype)stickLonger;

Implement this method in Stick. m:

+(instancetype)stickLonger{    Stick *stick = (Stick*)[CCBReader load:@Elements/StickLonger];    stick.name = @stickLonger;    return stick;}

The name of the stick object is different from that of the common stick object.

Return to GameScene. m and add the following sentence to the collision processing between ball and Brick:

[Star spawnStar:(Brick*)brick];

Next, we want to deal with the events when the stars and bounce sticks are in touch:

-(BOOL) ccPhysicsCollisionBegin :( CCPhysicsCollisionPair *) pair star :( CCNode *) star stick :( CCNode *) stick {// star is a pentagram and may collide multiple times in a short time; however, if the star is deleted from the first collision, the star is nil. // make sure that the star is not nil. // The deletion of star cannot guarantee that it is set to nil. Therefore, you can add a parameter to determine whether its parent is nil. (2006) StarType starType; @ synchronized (self) {if (! Star |! Star. parent) {return YES;} starType = (Star *) star ). starType; [star removeFromParentAndCleanup: YES];} switch (starType) {case starTypeStickLonger: @ synchronized (self) {[self scheduleBlock: ^ (CCTimer * timer) {[Star doStickLongerWork: self. stickInGameScene];} delay: 0];} break; case starTypeUnknown: case starTypeMax: NSAssert (NO, @ error star type !); Break; default: break;} return YES ;}

Why is the synchronous pseudo-command used? Because the code changes the status of the GameScene bounce rod, and the status may be changed at the same time in GameScene, we need to perform synchronous processing.

Extended Star class

Finally, we add the doStickLongerWork Method to the Star class:

+(void)doStickLongerWork:(Stick *)stick{    GameScene *gameScene = [GameScene sharedGameScene];    CCPhysicsNode *physicsWorld = (CCPhysicsNode*)stick.parent;    @synchronized(gameScene){        if ([stick.name isEqualToString:@stickLonger]) {            return;        }        if ([stick.name isEqualToString:@stickShorter]) {            Stick *stickNormal = [Stick stickNormal];            stickNormal.position = stick.position;            [stick removeFromParent];            //[physicsWorld removeChild:stick cleanup:YES];            [physicsWorld addChild:stickNormal];            gameScene.stickInGameScene = stickNormal;            return;        }    }    CGPoint position = stick.position;    __block Stick *stickLonger;    @synchronized(gameScene){        stickLonger = [Stick stickLonger];        //[physicsWorld removeChild:stick cleanup:YES];        [stick removeFromParent];        stickLonger.position = position;        [physicsWorld addChild:stickLonger];        stickLonger.visible = NO;        gameScene.stickInGameScene = stickLonger;        CCSprite *stickNode = (CCSprite*)[CCBReader load:@Elements/StickNode];        stickNode.position = stickLonger.position;        [gameScene addChild:stickNode z:50];        CCActionScaleTo *longerAction = [CCActionScaleTo actionWithDuration:0.4f scaleX:2.0f scaleY:1.0f];        CCActionCallBlock *blk = [CCActionCallBlock actionWithBlock:^{            [stickNode removeFromParent];            stickLonger.visible = YES;        }];        CCActionSequence *seq = [CCActionSequence actions:longerAction,blk,nil];        [stickNode runAction:seq];    }    [stickLonger scheduleBlock:^(CCTimer *timer){        @synchronized(gameScene){            Stick *stickNormal = [Stick stickNormal];            stickNormal.position = stickLonger.position;            [stickLonger removeFromParent];            [physicsWorld addChild:stickNormal];            gameScene.stickInGameScene = stickNormal;        }    } delay:10];}

The first thought of the above method is estimated to be: Long! In fact, the content is well understood. Basically, it has done the following:

If the rebound rod has become longer, nothing will be returned. If the rebound rod is in a short state, the original size will be restored. here we have considered other overlapping effects that may change the rebound rod. we will also consider this point in the Code logic of the rebound rod becoming shorter. create a variable-length bounce stick object, but hide it for the moment, because we also want to achieve a rebound rod from short-length animation effect. this animation is very short, so it does not affect user control. if you want to achieve the continuity of user control more perfectly, You can "graft" the bounce Rod's touch processing to this temporary object. Of course, this is a little troublesome, due to the length of the article, this is just the end of the period. restore the bounce rod to its original size in 10 seconds.

After the variable length prop is created, we will compile and run the app to see the effect:

 

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: info-contact@alibabacloud.com 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.