IOS_31_cocos2d _ message Scheduling

Source: Internet
Author: User

IOS_31_cocos2d _ message Scheduling

Final effect:



CocZ tables? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> cipher/cipher + CjxwPr7Nu + cipher + Zm9yZWFjaCAgILvy1d/cipher + cw.vcd4kpha + pgltzybzcm9" http://www.2cto.com/uploadfile/Collfiles/20140908/2014090808150452.png "alt =" \ ">








The encapsulated bullet class inherited from CCSprite.

//// Bullet. h // 31_cocos2D getting started /// Created by beyond on 14-9-7. // Copyright (c) 2014 com. beyond. all rights reserved. // bullet member attribute: the speed of movement per second # import "CCSprite. h "@ interface Bullet: CCSprite // speed (moving this speed per second) @ property (nonatomic, assign) CGPoint velocity; @ end



//// Bullet. m // 31_cocos2D getting started /// Created by beyond on 14-9-7. // Copyright (c) 2014 com. beyond. all rights reserved. // # import "Bullet. h "@ implementation Bullet // do nothing in cocos2d v3-(id) initWithTexture :( CCTexture *) texture rect :( CGRect) rect {if (self = [super initWithTexture: texture rect: rect]) {// update: // [self schedule: @ selector (update :) interval: 0]; // Cancel message scheduling // [self unschedule: <# (SEL) #>];} return self;} // distance = speed * time // The time interval between two frames in delta-(void) update :( CCTime) delta {// increase the x value by 5 at a time // self. position = ccpAdd (self. position, ccp (5, 0); // s = speed * time CGPoint deltaS = ccpMult (_ velocity, delta); // change the position self. position = ccpAdd (self. position, deltaS) ;}@ end


Main scenarios



//// HelloWorldScene. h // 31_cocos2D getting started /// Created by beyond on 14-9-5. // Copyright com. beyond 2014. all rights reserved. //// Importing cocos2d. h and cocos2d-ui.h, will import anything you need to start using Cocos2D v3 # import "cocos2d. h "# import" cocos2d-ui.h "/*** main scenario */@ interface HelloWorldScene: CCScene + (HelloWorldScene *) scene;-(id) init; @ end





//// HelloWorldScene. m // 31_cocos2D getting started /// Created by beyond on 14-9-5. // Copyright com. beyond 2014. all rights reserved. // # import "HelloWorldScene. h "# import" IntroScene. h "# import" CCAnimation. h "// Bullet # import" Bullet. h "@ implementation HelloWorldScene {CCSprite * _ sprite;} # pragma mark-lifecycle + (HelloWorldScene *) scene {return [self alloc] init];} // In HelloWorldScene-(id) init {if (! (Self = [super init]) return (nil); // 1. Scene Node allows interaction with self. userInteractionEnabled = YES; // 2. The background color is dark gray. CCNodeColor * background = [CCNodeColor nodeWithColor: [CCColor colorWithRed: 0.2f green: 0.2f blue: 0.2f alpha: 1.0f]; [self addChild: background]; // 3. Add an Genie and center it in the center [self addSpriteInCenter]; // 4. Create a return button in the upper right corner, return to the first scenario [self addBtnOnTopRight]; // 5. Important: Enable message calling // [self schedule: @ selector (update :) interval: 0]; // return the created scene object return self;} # pragma mark-Enter & Exit //-(void) onEnter {// always first call the onEnter method of the parent class [super onEnter]; // In pre-v3, touch enable and scheduleUpdate was called here // In v3, touch is enabled by setting userInteractionEnabled for the individual nodes // Per frame update is automatically enabled, if update is overridden}-(void) onExit {// The onExit method [super onExit] of the parent class must always be called at the end;} # pragma mark-touch event // The user can touch the screen, and the genie can follow his finger to move-(void) touchBegan :( UITouch *) touch withEvent :( UIEvent *) event {CGPoint touchLoc = [touch locationInNode: self]; // output the coordinate CCLOG of the touch point (@ "Move sprite to @ % @", NSStringFromCGPoint (touchLoc )); // move the genie To the touch point To indicate absolute By To indicate relative // CCActionMoveTo * actionMove = [CCActionMoveTo actionWithDuration: 1.0f position: touchLoc]; // call the runAction method of the genie to execute the action. // [_ sprite runAction: actionMove];} // put a bullet when you hold it up and let the bullet go, and perform Collision Detection-(void) touchEnded :( UITouch *) touch withEvent :( UIEvent *) event {// get the touch point CGPoint touchLoc = [touch locationInNode: self]; bullet * bullet = [Bullet spriteWithImageNamed: @ "bullet.png"]; bullet. name = @ "bullet"; // set the position where the bullet appears. position = touchLoc; // set the speed of the bullet. velocity = ccp (500, CCRANDOM_MINUS1_1 () * 300); [self addChild: bullet] ;}// perform collision detection in the message scheduling in the scenario-(void) update :( CCTime) delta {[self boundaryCheck];}-(void) boundaryCheck {for (int I = 0; I
 
  











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.