Cocos2d 3.3 lua Camera, cocos2dlua

Source: Internet
Author: User

Cocos2d 3.3 lua Camera, cocos2dlua
1. Why use Camera?

Games are a bit like movies, and lens usage is very important. With the main character, scenario switching, etc. In general, 2d games do not need to use Camera, so cocos2d does not pay too much attention to Camera, so it is easier to use Camera until 3.3. Previously, some effects can be simulated using other methods, but the logic is not intuitive, and direct operations on Camera are perfect.


Let's take a look at our Camera Demo:


According to common sense, a person keeps moving up (I am flying, haha ).

There are two game implementation methods:

1. Fixed the position where the character was located, and then moved the background down. It looks like "The character is moving up"

2. The characters move up. The Camera position is below the character's position, which is a distance from the character and moves along with the movement of the character. The background is completely unchanged but will be spliced.

Before Cocos2d 3.3, we can only implement it in a way similar to the first method. A visual deception. With Camera, we can implement it in the second way, which is more logical.


2. Create 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)

We all know that opengl APIs have two types of Camera. One is the same as a Camera in real life. In the distance, things will be smaller and closer to each other. The other is that the distance is as big as the distance. This is a 2d game, so we only need to use the second Camera.

CreateOrthographic has four parameters, which roughly specifies the range of the Camera. If the 3d space is so large, it will consume a lot of observation and it is not necessary. Therefore, Camera has a concept of observation range. The four parameters indicate the width, height, proximity, and distance respectively. Because it is a 2d game, it is OK if 0 is included in the near and distant areas. If your game scenario is not properly displayed, note whether it contains 0.


SetCameraFlag corresponds to setCameraMask. It determines which things will be seen by 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,};

We know from the source code that USER1 = 2, USER2 = 4, etc. If you set the CameraMask value of node to the same as that of CameraFlag, this node is visible to Camera.

You can set the cameramask of a layer so that all nodes in the layer are recursively set. Pay attention to the setting time. You must set it at the end so that the node added earlier will take effect.

Note the following:

Because Camera is used, do not forget to set cameramask for new things when adding something in the game. Otherwise, you will not be able to see it!


3. nodes not restricted by Camera

Have you noticed the pause button in the upper right corner? Its position is fixed without any movement. How is it done? It is very simple. You can simply do it without setting cameramask. Isn't it because you can't see cameramask without setting it? How can we see it again, and the location remains unchanged ?. This is complicated. It is related to the camera implementation of the cocos2d game engine. It is so complicated that I have not understood it yet.


4. Background Splicing

Although this article focuses on Camera, we will talk about the background processing here. Because Camera is used, the background does not need to be moved. However, when Camera moves up, the background will be left behind and there will be no background. Therefore, the background must be constantly changed.

When can I change the location? When the top of the first background is under the Camera position, you can move the background image to the top of all 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 only have two background images, I still use table for storage. This facilitates expansion. You can also use 3 or 4 background images conveniently. For specific implementation, see the source code of this Demo.


5. Take a look at the specific use of camera

This is a new game. With camera in cocos2d 3.3, it will be clearer.

IPhone download: https://itunes.apple.com/cn/app/30-wait/id954181532? L = zh & mt = 8 download Android: http://toycloud.qiniudn.com/stickhero.apkscan QR code download more easily.


6. Download the Camera Demo Source Code mentioned above


Http://www.waitingfy.com /? Attachment_id = 1487

 

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


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.