local png = "2.png"--文件路径 local sprite = cc.Sprite:create(png)--,cc.rect(0,0,0,0)) self:addChild(sprite) sprite:setPosition(display.width/2,display.height/2)
--实现事件触发回调 localfunctiononTouchBegan(touch, event) local target = event:getCurrentTarget()
local locationInNode = target:convertToNodeSpace(touch:getLocation()) local s = target:getContentSize() local rect = cc.rect(0, 0, s.width, s.height)
if cc.rectContainsPoint(rect, locationInNode) then returntrue end returnfalse end localfunctiononTouchMoved(touch, event) end localfunctiononTouchEnded(touch, event) print("触摸事件") sprite:setPosition(300,500) end
--事件响应方法 functionMainScene:onEvent(sender,event) print("1234") -- if event == ccui.PageViewEventType.turning then -- local pageNum = sender:getCurrentPageIndex() -- print("is turning,this PageNum:"..pageNum) -- end end
local sprite = cc.Sprite:create("")--自己随便找个图片试一试 localfunctiononTouchBegan( touch, event ) local point = touch:getLocation() --获取鼠标坐标 local rect = sprite:getBoundingBox() -- 获取精灵的范围 if (cc.rectContainsPoint(rect,point)) then--判断鼠标是否在精灵范围内,是触发 returntrue; end returnfalse --这里必须判断,不然拖动屏幕精灵也会移动 end localfunctiononTouchEnded( touch, event ) -- body end localfunctiononTouchMoved(touch, event) local target = sprite --获取当前的控件(这里是精灵) local posX,posY = target:getPosition() --获取当前的位置 local delta = touch:getDelta() --获取滑动的距离 target:setPosition(cc.p(posX + delta.x, posY + delta.y)) --给精灵重新设置位置 end local listener1 = cc.EventListenerTouchOneByOne:create() --创建一个单点事件监听 listener1:setSwallowTouches(true) --是否向下传递 listener1:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) listener1:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) listener1:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) local eventDispatcher = self:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(listener1, sprite) --分发监听事件
获取鼠标点击位置
1 2 3 4
local location = touch:getLocation() --获取鼠标的位置 local event_x = location["x"] or0 local event_y = location["y"] or0 print("event_x = " .. event_x .. " event_y = " .. event_y)
游戏暂停
说明,点击“go”按钮,按钮变成“stop”
1 2 3 4 5 6 7 8 9 10 11
local spriteFrame = cc.SpriteFrameCache:getInstance() spriteFrame:addSpriteFrames("Plist.plist") local goSprite = cc.Sprite:createWithSpriteFrameName("go.png") local stopSprite = cc.Sprite:createWithSpriteFrameName("stop.png") local goToggleMenuItem = cc.MenuItemSprite:create(goSprite, goSprite) local stopToggleMenuItem = cc.MenuItemSprite:create(stopSprite,stopSprite) local toggleMenuItem = cc.MenuItemToggle:create(goToggleMenuItem,stopToggleMenuItem) toggleMenuItem:setPosition(cc.Director:getInstance():convertToGL(cc.p(930,540))) local a = cc.Menu:create(toggleMenuItem) a:setPosition(cc.p(0,0)) self:addChild(a)
local p1 = {x = x1 , y = y1} local p2 = {x = x2 , y = y2 } self:getAngleByPos(p1,p2) functionGameScene:getAngleByPos(p1,p2) local p = {} p.x = p2.x - p1.x p.y = p2.y - p1.y local r = math.atan2(p.y , p.x) * 180/math.pi print("夹角为:" .. r) end
画二次贝塞尔曲线
1 2 3 4 5 6 7 8 9
functionGameScene:testBezier() ifnotself.drawNode then self.drawNode = cc.DrawNode:create(). --创建节点 self:addChild(self.drawNode) end self.drawNode:clear() --删除节点 self.drawNode:drawQuadBezier(cc.p(self.xp0,self.yp0),cc.p(self.xp1,self.yp1),cc.p(self.xp2,self.yp2),1000,cc.c4f(1,1,0,1)) end --p0,p1,p2 分别为曲线起点,控制点,终点
local run1 = self:runAction(cc.RotateTo:create(1,-80)) local run2 = self:runAction(cc.RotateTo:create(1,80)) local delay = cc.DelayTime:create(0.01) self:runAction(cc.RepeatForever:create(cc.Sequence:create(run1,delay,run2,delay)))