【Cocso2d-x Lua笔记五】quick中的display

display负责与显示图像、场景有关的功能

display 模块封装了绝大部分与显示有关的功能,并负责根据 config.lua 中定义的分辨率设定计算屏幕的设计分辨率。

下面通过一个小例子来了解它

1.新建一个Cocos Quick工程

main.lua

function __G__TRACKBACK__(errorMessage)
    print("----------------------------------------")
    print("LUA ERROR: " .. tostring(errorMessage) .. "\n")
    print(debug.traceback("", 2))
    print("----------------------------------------")
end

--[[后台输出记录]]
local cclog= function(...)
  print(string.format(...))
end

package.path = package.path .. ";src/"
cc.FileUtils:getInstance():setPopupNotify(false)
require("app.MyApp").new():run()


在入口文件中最重要的就是require("app.MyApp").new():run()

 

MyApp.lua

require("config")
require("cocos.init")
require("framework.init")
require("framework.shortcodes")
require("framework.cc.init")

local MyApp = class("MyApp", cc.mvc.AppBase)

function MyApp:ctor()
    MyApp.super.ctor(self)
end

function MyApp:run()
    cc.FileUtils:getInstance():addSearchPath("res/")
    self:enterHomeScene()
end

function MyApp:enterMainScene()
     self:enterScene("MainScene")
end

function MyApp:enterMainSceneFade()
     self:enterScene("MainScene",nil,"fade",0.6,cc.csb(0,0,0))
end

function MyApp:enterHomeScene()
     self:enterScene("HomeScene",nil,"fade",0.6,cc.c3b(0,0,0))
     
end 

function MyApp:exit()
	os.exit()
end

return MyApp


MyApp继承了quick中的模块cc.mvc.AppBase。AppBase是一个基础应用类。在MyApp的构造函数中一定定要继承父类的方法MyApp.super.ctor(self)。enterScene用来进入场景,可以在其中设置切换的动画。self:enterScene("MainScene",nil,"fade",0.6,cc.csb(0,0,0))表示时间为0.6秒的渐隐效果,背景黑色。

 

HomeScene.lua

--创建游戏主页场景
local HomeScene = class("HomeScene", function()
    return display.newScene("HomeScene")
end)

function HomeScene:ctor()
	self:addElement()
end

function HomeScene:addElement()
   
   self.MainBg=display.newTilesSprite("Background/day_buttom.png",cc.rect(display.left,display.bottom,display.width,display.height)):addTo(self)
    --align(target, anchorPoint, x, y)
   self.MainBottomBg=display.newSprite("Background/day_tree_buttom.png"):align(display.BOTTOM_CENTER,display.cx,display.bottom):addTo(self)  
   self.MainTitleBg=display.newSprite("Background/day_title_buttom.png"):align(display.TOP_CENTER,display.cx,display.top-80):addTo(self)
   self.MainRabbitBg=display.newSprite("Background/day_rabbit_buttom.png"):align(display.BOTTOM_CENTER,220,display.bottom+20):scale(0.8):addTo(self)
end



function HomeScene:addMenuElement()


end

function HomeScene:onEnter()
end

function HomeScene:onExit()
end


return HomeScene

display.newScene("HomeScene")返回一个名为“HomeScene”的Scene。

self.MainBg=display.newTilesSprite("Background/day_buttom.png",cc.rect(display.left,display.bottom,display.width,display.height)):addTo(self)创建并返回一个平铺的Sprite对象,(图像名,平铺的范围)。它的功能有点类似于.9.png可以对图片进行缩放。其中display.left,display.bottom,display.width,display.height为屏幕的左、下、宽、高。addTo(self)就是将创建好的sprite加入到HomeScene中。相当于HomeScene.addChild(MainBg)。

self.MainBottomBg=display.newSprite("Background/day_tree_buttom.png"):align(display.BOTTOM_CENTER,display.cx,display.bottom):addTo(self)  align用来对齐

align(target, anchorPoint, x, y)由于在Lua语言中使用“:”会将自身作为参数传入函数中,所以target参数省略不填,anchorPoint为对象设置锚点,如display.BOTTOM_CENTER将图片的底部终点作为锚点,并非屏幕的底部终点,切记。

 

看见display是一个用于管理显示功能的模块,不是属于哪个类的。它的功能还有很多,就不一一例举了。

详见http://cn.cocos2d-x.org/article/index?type=quick_api&url=/doc/api_quick/display.html#anchor_display_newScene

 


 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值