Cocos2d-iphone magic tower layer 20 Part 3

Source: Internet
Author: User

First, we need to add a direction controller. First, the Class header file Game01 defines four attributes [html] @ property (nonatomic, retain) CCSprite * btnup; @ property (nonatomic, retain) CCSprite * btndown; @ property (nonatomic, retain) CCSprite * btnleft; @ property (nonatomic, retain) CCSprite * btnright; define an instance variable CCSprite * btnnormal; then, define two global variables [html] In the Game01.m file // move the int ction; CGPoint point; add the following code [html] in the initialization method // direction control point = CGPointMake (size. width-110, 75 ); Btnnormal = [CCSprite spriteWithFile: @ "btn_normal.png"]; self. btnup = [CCSprite spriteWithFile: @ "btn_up.png"]; self. btndown = [CCSprite spriteWithFile: @ "btn_down.png"]; self. btnleft = [CCSprite spriteWithFile: @ "btn_left.png"]; self. btnright = [CCSprite spriteWithFile: @ "btn_right.png"]; btnnormal. position = self. btnup. position = self. btndown. position = self. btnleft. position = self. btnright. positio N = point; [self addChild: btnnormal, click different regions to switch between different controller images [html] # pragma mark-in-game touch response events-(BOOL) ccTouchBegan :( UITouch *) touch withEvent :( UIEvent *) event {// obtain the position of the touch point CGPoint touchLocation = [self convertTouchToNodeSpace: touch]; // obtain the CGRect Rect = CGRectMake (touchLocation. x, touchLocation. y, 1, 1); CGRect RectUp = CGRectMake (point. x-20, point. y + 20, 40, 65 ); CGRect RectDown = CGRectMake (point. x-20, point. y-85, 40, 65); CGRect RectLeft = CGRectMake (point. x-85, point. y-20, 65, 40); CGRect RectRight = CGRectMake (point. x + 20, point. y-20, 65, 40); if (! _ Hero. isFighting) {direction = 0; // check whether the contact is in the control zone if (CGRectIntersectsRect (Rect, RectUp) {direction = 1; curbtn = self. btnup; [self addChild: self. btnup];} if (CGRectIntersectsRect (Rect, RectDown) {direction = 2; curbtn = btndown; [self addChild: btndown];} if (CGRectIntersectsRect (Rect, RectLeft )) {direction = 3; curbtn = btnleft; [self addChild: btnleft];} if (CGRectIntersectsRect (Rect, RectRi Ght) {direction = 4; curbtn = btnright; [self addChild: btnright] ;}return YES ;}- (void) ccTouchEnded :( UITouch *) touch withEvent :( UIEvent *) event {direction = 0; [self removeChild: curbtn cleanup: YES];} Do not forget to enable touch before returning the initialization method. Here I use [CCTouchDispatchersharedDispatcher] addTargetedDelegate: selfpriority: 0 swallowsTouches: YES]; when the touch event is enabled, you will find that the Warriors still cannot move, then let's connect the controller with the warrior's mobile phone. I use an update method to update the warrior's mobile location [html]-(void) UpdateMove {CGPoint playerPoint = _ hero. position; switch (direction) {case 1: playerPoint = CGPointMake (playerPoint. x, playerPoint. y + 32 * _ scale); break; case 2: playerPoint = CGPointMake (playerPoint. x, playerPoint. y-32 * _ scale); break; case 3: playerPoint = CGPointMake (playerPoint. x-32 * _ scale, playerPoint. y); break; case 4: playerPoint = CGPointMake (playerPoint. x + 32 * _ scale, playerPoint. y ); Break; default: break;} if (playerPoint. x <= RIGHTMAX) & (playerPoint. x> = LEFTMIN) & (playerPoint. y <= UPMAX) & (playerPoint. y> = DOWNMIN) {[self setPlayerPosition: playerPoint] ;}}- (void) setPlayerPosition :( CGPoint) position {www.2cto.com _ hero. position = position;} the four variables "rightmax upmax downmin leftmin" are used to control the upper and lower margins of the warrior's moving range. Before the touch event is enabled, add [selfschedule: @ selector (updateMove) interval: 0.1]; so that our warriors can move But you will find that the warriors can move freely on the map, which obviously does not work. Next we will add the warriors mobile detection, we must know the block information on the next Moving position, such as whether there are enemies or walls. In this way, we must parse the map. Maybe you have noticed that there are two warriors on the map. In fact, the two are the warrior location map blocks that crossed the floor when creating the map, in the resolution map, we will delete the two blocks. [Plain] <pre style = "margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb (240,240,240) "> </pre>>

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.