zoukankan      html  css  js  c++  java
  • Cocos2dx 学习笔记(4) 对笔记3中触摸控制的第二种实现

    从一位大侠的博客上看到,说手游尽可能的使用脚本,可以便于版本更新,觉得很有道理,每次更新执行文件太蛋疼了,所以从设计初就应该考虑进来.

    比如我刚写的场景控制类,以后各种触发肯定跟着需求随时变动,但又不想写过多的脚本回调,那就直接挪到脚本好了,只要控制的好,效率还是可以保证的.

    而主场景还是在C++中实现,便于数据管理.

    主控脚本

    --------------------------------------------------------
    --文件名: control_iso.lua
    --描  述: 等轴视距场景控制
    --日  期: 11/3/2013
    --作  者: KevinYuen
    --版  权: Copyright (C) 2011 - All Rights Reserved
    --备  注:
    ---------------------------------------------------------
    
    -- 绑定的场景
    local bind_scene = nil;
    
    -- 获取指定节点坐标的地块索引
    -- 方法来自网络
    function tilePosFromLocation( location, tileMap )
    
    	-- Tilemap position must be subtracted, in case the tilemap position is scrolling
    	local x, y = tileMap:getPosition();
    	local pos = ccpSub( location, CCPointMake(x,y) );
    	local halfMapWidth = tileMap:getMapSize().width * 0.5;
    	local mapHeight = tileMap:getMapSize().height;
    	local tileWidth = tileMap:getTileSize().width;
    	local tileHeight = tileMap:getTileSize().height;
    	local tilePosDiv = CCPointMake(pos.x / tileWidth, pos.y / tileHeight);
    	local inverseTileY = mapHeight - tilePosDiv.y;
    	-- Cast to int makes sure that result is in whole numbers
    	local posX = math.floor( inverseTileY + tilePosDiv.x - halfMapWidth );
    	local posY = math.floor( inverseTileY - tilePosDiv.x + halfMapWidth );
    	-- make sure coordinates are within isomap bounds
    	posX = math.max(0, posX);
    	posX = math.min(tileMap:getMapSize().width - 1, posX);
    	posY = math.max(0, posY);
    	posY = math.min(tileMap:getMapSize().height - 1, posY);
    	return CCPointMake(posX, posY);
    
    end
    
    -- 创建控制
    function createControl( scene )
    
    	bind_scene = scene;
    	if not bind_scene then return end;		-- 绑定的场景
    
        local touchBeginPoint = nil;			-- 拖拽坐标记录
    	local touchSceneDragged = false;		-- 是否拖拽过地图了
    
    	-- 触摸开始
    	local function onTouchBegan( x, y )
    		touchBeginPoint = {x = x, y = y};	-- 位置记录
    		touchSceneDragged = false;			-- 重新开始记录
    		return true;
    	end
    
    	-- 触摸结束
    	local function onTouchEnded( x, y )
    
    		-- 没有绑定的场景返回
    		if not bind_scene then return end;
    
    		-- 如果拖拽过地图,那么就不进行拣选操作
    		if touchSceneDragged then return end;
    
    		-- 地图层拣选
    		local mapLayer = bind_scene:getMapLayer();
    		if mapLayer then
    			-- 坐标转换
    			local tmxNode = mapLayer:layerNamed( "ground" );
    			if tmxNode then
    				local Location = tmxNode:convertToNodeSpace( CCPointMake( x,y ) );
    				local tilePos = tilePosFromLocation( Location, mapLayer );
    				--local tilePos = CIsometricController:tilePosFromLocation( Location, mapLayer );
    				local tileSprite = tmxNode:tileAt( tilePos );
    				if tileSprite then
    					-- 隐藏选中的地块
    					tileSprite:setVisible( false );
    					return;
    				end
    			end
    		end
    
    	end
    
    	-- 触摸移动
    	local function onTouchMoved( x, y )
    
    		if not bind_scene or not touchBeginPoint then return end;
    
    		local cx, cy = bind_scene:getPosition();
    		bind_scene:setPosition( cx + x - touchBeginPoint.x, cy + y - touchBeginPoint.y );
    		touchBeginPoint = {x = x, y = y};
    		touchSceneDragged = true;	-- 地图拖拽过了
    
    	end
    
    	-- 触摸事件处理
    	local function onTouch( eventType, x, y )
    
    		if eventType == CCTOUCHBEGAN then
    			return onTouchBegan(x, y)
    		elseif eventType == CCTOUCHMOVED then
    			return onTouchMoved(x, y)
    		else
    			return onTouchEnded(x, y)
    		end
    
    	end
    
    	-- 注册场景触摸事件脚本通告的相关事项
    	local controlLayer = CCLayer:create();
    	controlLayer:registerScriptTouchHandler( onTouch );
    	controlLayer:setTouchEnabled( true );
    
    	bind_scene:addChild( controlLayer, 0, ESLT_CONTROL );
    
    end
    

     明天开始学习动画,设计游戏对象类,看看如何封装好~先给张图:

  • 相关阅读:
    团队第五次作业评分 项目冲刺
    团队第四次作业评分 系统设计和数据库设计
    随笔
    爬虫
    javascript简单分页
    cookie的存储与取值
    redis的五种数据类型
    什么是反射
    小王同学的随笔
    something just like this---About Me
  • 原文地址:https://www.cnblogs.com/KevinYuen/p/2954804.html
Copyright © 2011-2022 走看看