Cocos2d-x game instances (16)-vertical version of the shooting game (3)

Source: Internet
Author: User

Small man (Bill man) Personal Original, welcome to reprint, reprint please indicate the address, small man (Bill man) column address http://blog.csdn.net/bill_man

The previous article sets up a simple structure for the shooting game objects of the vertical version, that is, all game objects (bullets, enemies, and protagonists) inherit from the same class. This article will further improve the game, collision between the residual image and the enemy when the protagonist moves;

1. Residual images of the protagonist's movement

When we move the main character, we can add a "shadow" effect to move the main character quickly. The effect is as follows:

The method is very simple, that is, when we create ccsprite, we create another ccmotionstreak, and move ccmotionstreak each time we move ccsprite, so that we can leave the effect of the vulnerability.

First, we can see at creation:

The first parameter is the retention time of the image, the second parameter is the number of parts of the image, the third parameter is the image path, and the fourth parameter is the width and height of the image, the last parameter is the color of the residual image. Because we use red as the main character, we also set it to red.

Then let's look at the setting location:

It's easy to set the location, that is, setpostion;

2. Add the enemy and detect the collision

As follows:

First, it is very easy to add the enemy. Just like adding the protagonist, we only need to add a movement to the enemy, that is, to control the movement of each frame of the enemy in tick, then we can call this tick in our main scenario. Then the collision detection is also carried out in the update of the main scenario.

First, let's look at the collision detection function collision:

This collision detection is very simple, it is to detect the collision between the enemy and the protagonist, that is, the collision between the enemy itself and the sprite of the main character. First, it checks whether the distance between the two rectangular centers is less than the distance between the two sides, then the two rectangles overlap. We need to correct the positions and correct the two rectangles to avoid collision. The coincidence of the rectangle is shown below:

If the two rectangles overlap, the abscissa difference of their centers must be less than the sum of the half width of their respective centers, and the ordinate difference must be less than the sum of the first half, that is, if (ABS (ownp. x-otherp. x) <= ownsize. width/2 + othersize. width/2 & ABS (ownp. y-otherp. y) <= ownsize. height/2 + othersize. height/2)

If there are any errors, I hope you can correct them more.

Next article continues the vertical version of the shooting game instance

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.