Panda Pig • Patty original or translated works. Welcome reprint, Reprint please indicate the source.
If you feel that the writing is not good please tell me, if you feel good please support a lot of praise. Thank you! Hopy;)
The Universal Star class has been completed, so we can achieve the specific length and shorten the props.
Variable length rebound stick
We want to do the following: After dropping a long star, if the rebound stick to it, then the length of the rebound stick is twice times the original.
It seems simple, but there is actually a problem. You can't just extend the length of the bounce stick sprite texture, because you're using the physics engine in this game, the size of the bounce stick's physical object is not changeable in the game running.
So we need to find a way to extend the physical size of the rebound bar, of course, but also to extend the size of the sprite frame, so as to match each other to achieve a realistic extension effect.
Here the cat uses the rescue method, using AI to make an extended rebound stick, and adjust its physical object to adapt to the new length, and then in the rebound stick need to grow, with a new long stick instead of the original short stick.
First make a long bounce stick with AI. Then create a STICKLONGER.CCB file in Spritebuilder and set the bounds of its physical object:
Achieve variable-length star props
Back in Xcode, add a new attribute to the interface file for the Stick class:
+(instancetype)stickLonger;
Implement this class of methods in STICK.M:
+(instancetype)stickLonger{ load:@"Elements/StickLonger"]; stick.name = @"stickLonger"; return stick;}
You can see that in addition to the stick object has changed, its name is also different from ordinary stick.
Back to GAMESCENE.M Add the following sentence to the collision handling of the ball and brick:
[Star spawnStar:(Brick*)brick];
Then we have to deal with the event when the star and the Bounce Stick touch:
-(BOOL) Ccphysicscollisionbegin: (Ccphysicscollisionpair *) pair star: (Ccnode *) Star stick: (Ccnode *) stick{The //star shape is a pentagram and may collide multiple times in a short period of time, but the star has been removed from the first collision, causing the star to be nil. //So make sure that star is not nil. //star Delete does not guarantee that it must be nil, so increase the judge whether its parent is nil. (2015-11-09 2006)Startype Startype; @synchronized ( Self){if(!star | |!star. Parent) {return YES; } Startype = ((star*) Star). Startype; [Star Removefromparentandcleanup:YES]; }Switch(Startype) { CaseStartypesticklonger: @synchronized ( Self){ [ Selfscheduleblock:^ (Cctimer *timer) {[Star dosticklongerwork: Self. Stickingamescene]; } Delay:0]; } Break; CaseStartypeunknown: CaseStartypemax:nsassert (NO, @"Error star type!"); Break;default: Break; }return YES;}
Why is synchronous pseudo-directive used? Because the code changes the state of the bounce bar in Gamescene, and the state can be changed at the same time in gamescene, we need to synchronize it.
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. Nameisequaltostring:@"Sticklonger"]) {return;} if ([Stick. Nameisequaltostring:@"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:sticknodeZ: -];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:Ten];}
You see the first feeling of the above method is estimated: Very long ah! In fact, the content is very good understanding, basically it does the following several things:
- If the rebound stick has become longer, do nothing to return
- If the rebound stick is in a shorter state, its original size is restored. Here are some additional effects that may change the rebound stick. In the code logic that bounces off the stick, we also take that into account.
- Create a variable-length bounce stick object, but temporarily hide it, because we also want to implement a bounce stick from short to long animation effect. Because this animation is short, it does not affect the user's control. If you want to achieve a more perfect user-controlled continuity, you can temporarily "graft" the bounce Stick's touch To this temporary object, of course, is a bit of trouble, space reason, here is just donuts.
- Restores the bounce stick to its original size after 10 seconds
Variable length props are finished, we now compile and run the app, look at the effect:
Next in the future to complete the shrinking props star, see you;)
(no.00004) iOS implementation brick Game (12): retractable freely, I am Ruyi Golden cudgel (ON)!