Cocos2d-x bullets

Source: Internet
Author: User

The function I want to implement is very simple, that is, by clicking any point on the screen, a bullet can be shot from the middle of the screen, and the bullet can fly out of the screen.

I used the CCMoveTo action, which requires a destination point. The destination point should be any point I clicked that is connected to a point outside the screen at the center of the screen. I defined the distance of 20 outside the screen as the end point. That is to say, the bullet will fly outside of 20, and the bullet will not be seen in this position.

Based on the linear function Y = kX + B, we need to find the values of k and B. We know two points, the starting point (the center point of the screen ), A point on a straight line (the point of the mouse), so that we can find k and B, so that we can determine this line. Then, according to the preset settings, the motion should be stopped as long as it reaches 20 outside the screen. This 20 refers to the X coordinate and Y coordinate. As long as one reaches 20, it should be stopped immediately. Otherwise, it is possible to go far and far away. In this way, I can use CCMoveTo to launch a bullet. I will write it as a function and use it directly in future projects. The Code is as follows:

CCPoint HelloWorld: GetTargetPointOutOfWorld (CCPoint ptStart, CCPoint ptEnd, int nXOutOfWorld, int nYOutOfWorld) {// Y = kX + B float fK = 1.0; float fb = 0.0; if (ptStart. x! = PtEnd. x) {fK = (float) (ptStart. y-ptEnd. y)/(ptStart. x-ptEnd. x); // obtain K} fb = ptStart. y-ptStart. x * fK; // obtain B // find the point of the line outside the screen CCSize size = CCDirector: sharedDirector ()-> getWinSize (); float fY = ptStart. y> ptEnd. y? -NYOutOfWorld: size. height + nYOutOfWorld; float fX = 1.0; if (fK! = 0) {fX = (fY-fb)/fK; // This fX may be very large, or very small} if (ptStart. x = ptEnd. x) // It should be moved along the Y axis {fX = ptStart. x; fY = ptStart. y> ptEnd. y? -NXOutOfWorld: size. height + nYOutOfWorld;} else if (ptEnd. y = ptStart. y) // should motion along the x axis {fX = ptStart. x> ptEnd. x? -NXOutOfWorld: size. width + nXOutOfWorld; fY = ptStart. y;} else if (fX> size. width + nXOutOfWorld) // recalculate fX and fY {fX = size. width + nXOutOfWorld; fY = fK * fX + fb;} else if (fX <-nXOutOfWorld) // recalculate fX and fY {fX =-nXOutOfWorld; fY = fK * fX + fb;} return ccp (fX, fY );}

The usage is as follows:

Bool HelloWorld: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {CCSprite * pSprite = CCSprite: create ("bullet.png"); // load the bullet image CCSize = CCDirector :: shareddire()-> getWinSize (); this-> addChild (pSprite); pSprite-> setPosition (ccp (size. width/2, size. height/2); // set the position of the bullet to const int OUT_OF_WORLD = 20; CCPoint ptOutOfWorld = GetTargetPointOutOfWorld (ccp (size. width/2, size. height/2), pTouch-> getLocation (), OUT_OF_WORLD, OUT_OF_WORLD); // obtain a point outside the screen. CCAction * pAction = CCMoveTo: create (0.5f, ptOutOfWorld ); pSprite-> runAction (pAction); // return true when a bullet is fired ;}

To achieve screen touch, you also need to register the touchable In the init function:

    this->setTouchEnabled(true);    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);

After reading some blog posts, many of them did not point out how to slash, and some were vertical lines or horizontal lines. I hope this article will help you with the desired shoes ~~~~

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.