(No. 00003) simple robot projection for iOS games (V)

Source: Internet
Author: User

(No. 00003) simple robot projection for iOS games (V)

In the previous article, we created a physical robot object. Next we will look at the corresponding logic code.

Go to Xcode and create a new Robot and Arm class, which inherit from CCNode and CCSprite classes respectively. Leave all the code blank and implement it later.

Let's take a look at how the robot interacts with players. when the player touches and moves the robot arm, the arm is rotated in the center of the joint. because the range of joint rotation is limited in SpriteBuilder, you don't have to worry about rotating the joint to a "strange" angle.

First, enable user interaction in the initialization method of Arm. m: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> self.userInteractionEnabled = YES;

Create an instance variable _ touchPoint in the Arm class:

@implementation Arm{    CGPoint _touchPoint;}

To add a touch callback method, the first method is touchBegan:

-(void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event{    CGPoint location = [[CCDirector sharedDirector] convertTouchToGL:touch];    _touchPoint = location;}

Here, the position at the first touch is used to compare with the coordinates at the next movement to determine the direction of selection.

Then the touchMoved method is used:

-(void)touchMoved:(CCTouch *)touch withEvent:(CCTouchEvent *)event{    CGPoint location = [[CCDirector sharedDirector] convertTouchToGL:touch];    MoveDirection direction = armMoveDirectionDown;    if (location.y > _touchPoint.y) {        direction = armMoveDirectionUp;    }else if(location.y < _touchPoint.y){        direction = armMoveDirectionDown;    }    [self moveArm:direction];}

Let's take a simple look at this method: Get the position where the touch moves, use it to compare with the previously saved position, as long as the value of the y axis is determined: If it is greater than the previous y value, it indicates the upward rotation, otherwise, it indicates downward rotation. finally, rotate the arm in the direction of rotation. here, the rotation direction is an enumeration value, because it may be used in other classes. For example, we will see a big problem in the rotation of the touch arm. You need to select the arm by touching the screen. we put it in a general header file and create a Comm. h header file, the content is as follows:

#ifndef ShootBall_Comm_h#define ShootBall_Comm_htypedef enum {    armMoveDirectionUp,    armMoveDirectionDown}MoveDirection;#endif

Then, you can include this header file in all the classes that need to be defined using this enumeration.

In the next article, we will see how the moveArm method is implemented in the code above ;)

 

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.