self 在lua中相当于java中的this

lua中的任何变量在没有赋值前, 都可以看做是nil

 lua变量有3种,
成员变量: self.变量名 = 
局部变量: local 变量名 = 
全局变量: 变量名 = 
 
在一个游戏中和逻辑有关的动作最好不用transition 因为不可控且不稳定
物体的移动可以用update的方法实现:

设置一个角色每帧移动的项目距离,比如speed = 5,然后解析几何的方法算出 speedX,和speedY的分量。
在update里,local curPos = self._imgSpr:getPositionInCCPoint()得到当前的坐标。坐标+速度,然后再setPosition就移动了。

例如:

 function Bullet:update()                    --总函数的update调用每个子弹的update

      local imagex = self._imgSpr:getPositionX();
local imagey = self._imgSpr:getPositionY(); imagex = imagex - self._speedx
imagey = imagey + self._speedy self._imgSpr:pos(imagex, imagey) end

lua中类的创建:

 local Bullet = class("Bullet", function()
return display.newNode()
end) speed = function Bullet:ctor() --构造函数
--bullet父对象是node 这里把sprite对象添加到node上
self._imgSpr = display.newSprite("img1.png") --添加bullet图片
self._imgSpr:pos(,)
self._imgSpr:scale(0.5)
self:addChild(self._imgSpr) self._speedx = nil
self._speedy = nil end function Bullet:init( x,y )
local mousex = x --bullet init传值 不推荐用构造函数传值 speed在这里计算出来后来直接调用即可
local mousey = y
local imagex =
local imagey = local distance = math.sqrt((mousex - imagex)*(mousex - imagex)+(mousey - imagey)*(mousey - imagey))
local degree = math.deg(math.asin((y - imagey)/distance))
if(mousex < imagex) then
degree = - degree
end
degree = - degree print(degree)
self._imgSpr:setRotation(degree) self._speedx = speed*math.cos(math.rad(degree))
self._speedy = speed*math.sin(math.rad(degree)) print(self._speedx) end

由上文可见此处的bullet类继承自node

ctor是构造函数,一般不建议传参数。若需要传参应新建一个函数 (如init)用于参数传递。

在main中新建实例时,首先要require声明此对象的文件。

例如新建bullet对象:

 ClassBullet = require("app/object/Bullet")                 -- require相当于include

 local bullet = ClassBullet.new()                           --bullet实例
bullet:init(x,y) --init bullet
self:addChild(bullet) --把bullet这个node添加到父节点中
table.insert(self._listBullet, bullet) --1开始 --把listBullet中对象insert到表中

第一版代码:

Bullet.lua

 local Bullet = class("Bullet", function()
return display.newNode()
end) speed = function Bullet:ctor() --构造函数
--bullet父对象是node 这里把sprite对象添加到node上
self._imgSpr = display.newSprite("img1.png") --添加bullet图片
self._imgSpr:pos(,)
self._imgSpr:scale(0.5)
self:addChild(self._imgSpr) self._speedx = nil
self._speedy = nil end function Bullet:init( x,y )
local mousex = x --bullet init传值 不推荐用构造函数传值 speed在这里计算出来后来直接调用即可
local mousey = y
local imagex =
local imagey = local distance = math.sqrt((mousex - imagex)*(mousex - imagex)+(mousey - imagey)*(mousey - imagey))
local degree = math.deg(math.asin((y - imagey)/distance))
if(mousex < imagex) then
degree = - degree
end
degree = - degree print(degree)
self._imgSpr:setRotation(degree) self._speedx = speed*math.cos(math.rad(degree))
self._speedy = speed*math.sin(math.rad(degree)) print(self._speedx) end function Bullet:update() --总函数的update调用每个子弹的update local imagex = self._imgSpr:getPositionX();
local imagey = self._imgSpr:getPositionY(); imagex = imagex - self._speedx
imagey = imagey + self._speedy self._imgSpr:pos(imagex, imagey) end return Bullet

MainScene.lua

local MainScene = class("MainScene", function()
return display.newScene("MainScene")
end) ClassBullet = require("app/object/Bullet") -- require相当于include
intersectLine = {left = display.c_left, right = display.c_right , top = display.c_top, bottom = display.c_bottom} function MainScene:ctor()
local layer = display.newLayer()
layer:setTouchEnabled(true) --响应touch事件
layer:addNodeEventListener(cc.NODE_TOUCH_EVENT, --添加监听器
function(event)
return self:onTouch(event.name, event.x, event.y, event.prevX, event.prevY)
end
)
layer:setTouchSwallowEnabled(false)
self:addChild(layer) self._imgBack = display.newSprite("back.jpg") --添加背景图片
self._imgBack:setAnchorPoint(ccp(,))
self:addChild(self._imgBack) self._listBullet = {} --bullet table self._scheduler = require("framework.scheduler") --事件调度器 与update搭配使用
self._scheduler.scheduleGlobal(handler(self, self.update), /) print(intersectLine.left,intersectLine.right)
end function MainScene:update()
for i,bullet in ipairs(self._listBullet) do --对listBullet表中的所有对象分别调用其update
bullet:update() local bulletx = bullet:getPositionX()
local bullety = bullet:getPositionY() print(bulletx,bullety) p = CCPoint(bulletx, bullety)
ifHit = hitR2P(intersectLine, p)
if ifHit == true then
table.remove(self._listBullet, i)
self:removeChild(bullet)
print("delete")
end end
end TouchEventString = TouchEventString or {} --------鼠标事件名称
TouchEventString.began = "began"
TouchEventString.moved = "moved"
TouchEventString.ended = "ended"
TouchEventString.canceled = "canceled" function MainScene:onTouch(name,x,y)
if name == TouchEventString.began then local bullet = ClassBullet.new() --bullet实例
bullet:init(x,y) --init bullet
self:addChild(bullet) --把bullet这个node添加到父节点中
table.insert(self._listBullet, bullet) --1开始 --把listBullet中对象insert到表中
end
return true
end function hitR2R(rect1,rect2)
if rect1 and rect2 then
if rect1.right > rect2.left and rect1.left < rect2.right and rect1.top > rect2.bottom and rect1.bottom < rect2.top then
return true
end
end
return false
end function hitR2P(rect,pos)
if pos.x <= rect.left or pos.x >= rect.right or pos.y <= rect.bottom or pos.y >= rect.top then
return true
end
return false
end return MainScene

此版代码可以实现基本的旋转和发射功能,但是并不能有效的remove飞出边界的bullet。

此处原因是对于Bullet的操作要注意不要直接操作class中的子节点,要以整个class为一个对象,在bullet层面上操作。

如此处update过程中

 local imagex = self._imgSpr:getPositionX();
local imagey = self._imgSpr:getPositionY();

操作的是self._imgSpr这个精灵类,并非是bullet的position发生变化。所以在MainScene的update方法中

 local bulletx = bullet:getPositionX()
local bullety = bullet:getPositionY()

print(bulletx,bullety) 得到的数总是0.

修改方法:

 self._imgSpr: 改成 self: 
 注意self. 和self:的区别。

心得:

在平时写代码的过程中要有意识的封装一些常用的函数。函数的复用性还是很高的。可以自己做一个functions.lua的文件。然后所有的通用函数都放这里,把这个文件require一下,就可以直接用里面的函数了。

出错的地方多写一些print帮助查错也是很有帮助的。

 CCNode里的这些方法 setPosition,setScale,setRotation 等等这类的方法,只要不重新set,这些值是不会变的,有些不需要变的值,不用每次update都去set,比如角度如果定好了不变,就不需要每次都去set了。
 
 另外。因为lua的特性。比如CCNode里有一个叫 aaa 函数,而你继承CCNode的时候,自己做了一个 self.aaa的变量。这样的话,父类的aaa函数就会失效了。。

所以我建议大家把类成员变量做一些特殊标记。比如我一般都会用 self._aaa
 
正确代码附录:
 local Bullet = class("Bullet", function()
return display.newNode()
end) speed = function Bullet:ctor() --构造函数
--bullet父对象是node 这里把sprite对象添加到node上
self._imgSpr = display.newSprite("img1.png") --添加bullet图片
self._imgSpr:pos(,)
self._imgSpr:scale(0.5)
self:addChild(self._imgSpr) self._speedx = nil
self._speedy = nil end function Bullet:init( x,y )
local mousex = x --bullet init传值 不推荐用构造函数传值 speed在这里计算出来后来直接调用即可
local mousey = y
local imagex =
local imagey = local distance = math.sqrt((mousex - imagex)*(mousex - imagex)+(mousey - imagey)*(mousey - imagey))
local degree = math.deg(math.asin((y - imagey)/distance))
if(mousex < imagex) then
degree = - degree
end
degree = - degree print(degree)
self._imgSpr:setRotation(degree) self._speedx = speed*math.cos(math.rad(degree))
self._speedy = speed*math.sin(math.rad(degree)) print(self._speedx) end function Bullet:update() --总函数的update调用每个子弹的update local imagex = self:getPositionX(); --只修改了Bullet的update函数
local imagey = self:getPositionY(); imagex = imagex - self._speedx
imagey = imagey + self._speedy self:pos(imagex, imagey) end return Bullet

Quick Cocos 旋转子弹的实现中我学到的的更多相关文章

  1. cocos2dx与Lua以及quick cocos

    1.cocos2dx中的脚本架构与组件 2.quick cocos的开发优势 3.自定义c++类如何导出到lua

  2. Quick Cocos (2.2.5plus)CoinFlip解析(MenuScene display AdBar二次封装)

    转载自:http://cn.cocos2d-x.org/tutorial/show?id=1621 从Samples中找到CoinFlip文件夹,复制其中的 res 和 script 文件夹覆盖新建工 ...

  3. 三维空间旋转和Three.JS中的实现

    三维空间中主要有两种几何变换,一种是位置的变换,位置变换和二维空间的是一样的.假设一点P(X1,Y1,Z1) 移动到Q(X2,Y2,Z2)只要简单的让P点的坐标值加上偏移值就可以了.但是三维空间的旋转 ...

  4. 【LeetCode】Find Minimum in Rotated Sorted Array 找到旋转后有序数组中的最小值

     本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4032570.html 原题: Suppose a sorted array is ...

  5. quick cocos 的scheduler 定时器

    cocos2dx原生lua对于定时器的写法: 1.每帧调用: void scheduleUpdateWithPriority(int priority) void scheduleUpdateWith ...

  6. quick Cocos 2dx 学习网站

    http://quick.cocoachina.com/wiki/doku.php?id=zh_cn http://www.cocoachina.com/ http://www.cocoachina. ...

  7. quick cocos 暂停场景

    local MainScene = class("MainScene", function() return display.newScene("MainScene&qu ...

  8. quick cocos map使用

    '''lua local MainScene = class("MainScene", function() return display.newScene("MainS ...

  9. LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

随机推荐

  1. 基于corosync+pacemaker+drbd+LNMP做web服务器的高可用集群

    实验系统:CentOS 6.6_x86_64 实验前提: 1)提前准备好编译环境,防火墙和selinux都关闭: 2)本配置共有两个测试节点,分别coro1和coro2,对应的IP地址分别为192.1 ...

  2. Django 前后台的数据传递

    Django 从后台往前台传递数据时有多种方法可以实现. 最简单的后台是这样的: from django.shortcuts import render def main_page(request): ...

  3. chrome中不可见字符引发的float问题

    起因是刷知乎时碰到这么个问题:https://www.zhihu.com/question/41400503 问题代码如下: <!DOCTYPE html> <html lang=& ...

  4. C#基础---Queue(队列)的应用

       Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...

  5. 手动配置三台虚拟机pacemaker+corosync并添加httpd服务

    创建三台虚拟机,实验环境:centos7.1,选择基础设施服务安装. 每台虚拟机两块网卡,第一块为pxe,第二块连通外网,手动为两块网卡配置IP.网关,使它们都能ping通外网并可以互相通过hostn ...

  6. Servlet编程

    Servlet编程 1. servlet概念及相关接口简介 java Servlet是运行在web服务器或应用服务器上的程序,他是作为来自web浏览器或其他HTTP客户端的请求和HTTP服务器山的数据 ...

  7. OnDraw函数

    本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6219428.html 方法一.对字符串直接赋值 在View类中定义了OnDraw函数 ...

  8. Eclipse CDT: Shortcut to switch between .h and .cpp

    ctrl+ tab  is the default shortcut.You can change it in Window → Preferences → General → Keys: Toggl ...

  9. 分布式监控系统Zabbix-3.0.3-完整安装记录(7)-使用percona监控MySQL

    前面已经介绍了分布式监控系统Zabbix-3.0.3-完整安装记录(2)-添加mysql监控,但是没有提供可以直接使用的Key,太过简陋,监控效果不佳.要想更加仔细的监控Mysql,业内同学们都会选择 ...

  10. 数组为什么可以使用linq查询

    问题引出 这视乎是个完全不必要进行讨论的话题,因为linq(这里具体是linq to objects)本来就是针对集合类型的,数组类型作为集合类型的一种当然可以使用了.不过我还是想写一下,这个问题源于 ...