(No. 00002) prototype of iOS game genie war (9)

Source: Internet
Author: User

(No. 00002) prototype of iOS game genie war (9)

Open MainScene. m and add the shoot method:

// The speed of a bullet is now related to the target distance. The closer the bullet speed is, the slower the bullet speed is. it should be changed to a constant speed. -(void) shoot :( CCNode *) player target :( CGPoint) point {CGPoint velocity = ccpSub (point, player. position); velocity = ccpMult (velocity, 50); if (ABS (velocity. x) <20 | ABS (velocity. y) <20) {velocity = ccpMult (velocity, 5);} if (ABS (velocity. x)> 200 | ABS (velocity. y)> 200) {velocity = ccpMult (velocity, 0.5);} CCNode * bullet = [self loadBullet]; NSAssert (bullet, @ bullet must not nil); bullet. position = player. position; CCPhysicsBody * physicsBody = bullet. physicsBody; [physicsBody applyAngularImpulse: 10]; [physicsBody applyImpulse: velocity];}

Let's talk about this method in detail. first, this parameter is passed in two parameters, corresponding to the emission point and the target point. the calculation shows that the bullet torque is velocity. because the default torque is too small and the force is too weak, we need to Properly enlarge the torque. the specific enlargement should be determined based on the actual App running effect. I think it is appropriate to increase the value by 50 times.

In the actual bullet launching process, the speed of a bullet may be too fast or too slow due to a small or too large corner of the torque. we need to adjust the torque value according to the actual angle. I admit that this is only a matter of expediency. If the time permits this judgment code to be further modified.

Then the bullet is loaded into the bullet. Here we make an asserted to prevent the bullet from being used. Normally, this should not happen.

Note that the loadBullet method has set the returned bullet to visible. Next we will set the initial position of the bullet, get the physical object of the bullet, and then apply the specified torque.

Another method applyAngularImpulse is also called in the torque method. This method is used to rotate the bullets and increase the visual 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.