zoukankan      html  css  js  c++  java
  • cocos2d-x笔记(十一)Lua发展飞机战争-5- 让飞机动

    然后在飞机上已被添加到游戏,下一步是让它动起来。主要是为了应对触摸事件。

    在C++通过重写ccTouchBegan()、ccTouchMoved()、ccTouchEnded()三个函数来响应触摸事件。

    在Lua器中就能够了。

    1.先设置该图层能够触摸。然后注冊响应函数onTouch

    	gameLayer:setTouchEnabled(true)
    	gameLayer:registerScriptTouchHandler(onTouch)

    2.onTouch函数有3个參数,第一个是事件的类型(began、moved、ended),后面两个參数就不用多说了。

    function onTouch(eventType, x, y)
    	if eventType == "began" then   
    		return onTouchBegan(x, y)
    	elseif eventType == "moved" then
    		return onTouchMoved(x, y)
    	else
    		return onTouchEnded(x, y)
    	end
    end

    3.分别实现3个触摸事件

    local touchBeginPoint = nil
    function onTouchBegan(x, y)
    	touchBeginPoint = {x = x, y = y}
    	return true
    end
    
    function onTouchMoved(x, y)
    	if PlaneLayer.alive() and PlaneLayer.containsTouchLocation(x,y) then 
    		--local offset = ccpSub(ccp(touchBeginPoint['x'],touchBeginPoint['y']),ccp(x,y))
    		--local toPoint = ccpAdd(ccp(PlaneLayer.getPlane():getPositionX(),PlaneLayer.getPlane():getPositionY()),offset)
    		PlaneLayer.moveTo(x,y)
    	end
    end
    
    function onTouchEnded(x, y)
    	
    end



    4.在onTouchMoved函数中出现了一个没见过的函数PlaneLayer.containsTouchLocation(x,y)。这种方法用来推断想触摸点是否在飞机的位置上。

    function containsTouchLocation(x,y)
    	local planeRect = plane:boundingBox()
    	planeRect.origin.x = planeRect.origin.x - 15
    	planeRect.origin.y = planeRect.origin.y - 15
    	planeRect.size.width = planeRect.size.width + 30
    	planeRect.size.height = planeRect.size.height + 30
        local b = planeRect:containsPoint(ccp(x,y))
        return b
    end


     

  • 相关阅读:
    电影投票使用到index索引 isdigit range += format upper
    for循环删除列表里的内容 删除字典中的内容
    3.格式化输出 format 三种方法集合% f
    列表和字符串的转换及statswith,endswith的应用判断
    过滤器,使用到in for break
    sort排序及reverse反转的结合使用
    列表内的改动
    django 第五天 自定义标签 静态文件
    Mysql 基础 1
    django 第四天模板渲染
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5039490.html
Copyright © 2011-2022 走看看