COCOS2D 3.3 Lua Camera use

Source: Internet
Author: User

1. Why to use camera

The game is a bit like a movie, the use of the lens is very important. Suddenly far and near, follow the protagonist, scene switch and so on. In general 2d games do not need to use the camera, so cocos2d didn't pay much attention to the camera, until 3.3 This version is easier to use the camera. Some of the previous effects can be used to emulate the camera in other ways, but this logic is less intuitive and the direct operation of the camera is perfect.


First look at our camera Demo:


It is common sense to understand that a person keeps moving upward (I am flying, haha).

There are 2 ways to implement the game:

1. Fix the character in that position, then move the background down so that it looks like "the character is moving up."

2. The character moves up, the camera position is below the character's position, distance from the character, follow the character movement and move, the background is completely motionless, but will be spliced.

Before Cocos2d 3.3, only a similar first way to achieve, a visual deception, this time with the camera we can be implemented in the second way, more logical.


2. Create a camera

if Self._camera = = Nil then        Self._camera = cc. Camera:createorthographic (Gameutil:visible_width (), Gameutil:visible_height (), 0, 1)        Self._camera: Setcameraflag (CC. Cameraflag.user1)        self:addchild (Self._camera)        self._camera:setposition3d (cc.vec3 (0, 0, 0))    end        Self:setcameramask (2)

I've done OpenGL. Some APIs know that there are two kinds of cameras, one is the same as the real life of the camera, the distant things will be smaller, near the big point. The other is the same size as the distance and near. This is a 2d game so just use the second camera here.

Createorthographic has 4 parameters, roughly specify the camera to see the range, 3d space so large, all the observation will be very expensive, also not necessary, so the camera has a concept of the scope of observation. These 4 parameters represent width, height, proximity, and distance, respectively. Because it is a 2d game, so near and far as long as the inclusion of 0 is OK. If your game scene does not display properly, note that there is no 0 included.


Setcameraflag and Setcameramask are a corresponding relationship. decided that things would be seen by the camera.

Enum class cameraflag{    DEFAULT = 1,    USER1 = 1 << 1,    USER2 = 1 << 2,    USER3 = 1 << 3,    USER4 = 1 << 4,    USER5 = 1 << 5,    USER6 = 1 << 6,    USER7 = 1 << 7,    USER8 = 1 << 8,};

look at the source code we know USER1 = 2, USER2 = 4 and so on, as long as the node Cameramask set with the Cameraflag value, this node is visible to the camera.

You can set the cameramask of a layer so that all node in the layer is recursively set. the timing of the setup should also be very careful, in the final setting, so that the previously added node will work.

Here's one to note:

Because of the use of camera, so in the game to add something, do not forget to give new things to set cameramask, otherwise it will not be visible!


3. Node that is not subject to camera restrictions

Notice the pause button in the upper-right corner? Its position is fixed and does not carry any movement. How did that happen? Very simple, do not set the cameramask on it. Not that you can't see it without setting the Cameramask? How can you see it here again, and the location is the same? It's complicated, it's all about the camera implementation of the COCOS2D game engine, it's too complicated, and I don't understand.


4. Stitching the background

Although this article is mainly about camera, here is the processing of the background. Because the camera is used, the background may not need to be moved. But when the camera moves up, the background is left behind and there is no background. So the background has to change the position constantly.

When do you change your position? When the top position of the first background is below the camera position, you can move the background image to the top of all the background images.

function Gamebackgroundlayer:getfirstbgtop () return (Self.allbackgrounds[1]:getpositiony ()  + self.height * 0.5) Endfunction gamebackgroundlayer:movefirstbgtolast () Local FIRSTBG = Self.allbackgrounds[1]table.remove ( Self.allbackgrounds, 1) table.insert (self.allbackgrounds, FIRSTBG) firstbg:setpositiony (self.allBackGrounds[# ( Self.allbackgrounds)-1]:getpositiony () + self.height) endfunction gameobjectslayer:update (DT)    if (Self._camera: Getpositiony () >= g_GameMainLayer.backgroundLayer:getFirstBgTop ()) then        G_gamemainlayer.backgroundlayer: Movefirstbgtolast ()    end end

Although I have only 2 background images here, I still use table to store them. This facilitates expansion. 3, 4 pictures of the background can also be very convenient to use. Concrete implementation look at this demo source code.


5. Take a look at the specific use of the camera

This is a new game, using the camera in Cocos2d 3.3, it will be clearer to play.

iphone Download: https://itunes.apple.com/cn/app/30-wait/id954181532?l=zh&mt=8 android Download:http://toycloud.qiniudn.com/StickHero.apkscanning QR code is easier to download.


6. The above mentioned camera demo source download


http://www.waitingfy.com/?attachment_id=1487

http://www.waitingfy.com/archives/1488


COCOS2D 3.3 Lua Camera use

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.