ccp之间是不可以直接进行+,-的,要用ccpSub和ccpAdd。

2024-06-24 11:08

本文主要是介绍ccp之间是不可以直接进行+,-的,要用ccpSub和ccpAdd。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 
http://www.cnblogs.com/buaashine/archive/2012/11/12/2765691.html 
上面有好多的关于数学的方面的知识,cocos2dx可能会用到的

2.学到了   根据tilemap坐标得到层上物体的id

int oneTiled=flagLayer->tileGIDt(tilePos);

还可以取得id对应的属性
CCDictionary* propertiesOnOneTile = tileMap->propertiesForGID(oneTileId);
const CCString* collide = propertiesOnOneTile->valueForKey("Collide");
const CCString* fruit = propertiesOnOneTile->valueForKey("fruit");

if(collide&&collide->compare("true")==0){
;
}
else if(fruit && fruit->compare("true") == 0){
//是一个西瓜
先取得对应的图层,然后再去掉西瓜,去掉障碍物
CCTMXLayer* fruitLayer = tileMap->layerNamed("foreground");
fruitLayer->removeTileAt(tilePos); // 吃掉西瓜 得分
flagLayer->removeTileAt(tilePos); // 移除障碍

CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pickup.wav");

this->_score++;
char str[40];
sprintf(str,"score:%d",_score);
scoreBord->setString(str);





3.源码在后面,以后用到这种逻辑的游戏的时候回来看。
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__


#include "cocos2d.h"


class HelloWorld : public cocos2d::CCLayer
{


private:
cocos2d::CCSprite* ninja;
cocos2d::CCTMXTiledMap*  tileMap;
cocos2d::CCTMXLayer* flagLayer;
cocos2d::CCLabelTTF* scoreBord;
int _score;
void setMapPosForView(cocos2d::CCPoint playerPos);


public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  


    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    
    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
cocos2d::CCPoint cocoscoord2tilemapcoord(cocos2d::CCPoint pos);


};


#endif // __HELLOWORLD_SCENE_H__


#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"


USING_NS_CC;


#define GET_TILE_WIDTH tileMap->getTileSize().width
#define GET_TILE_HEIGHT tileMap->getTileSize().height




#define MAP_WIDTH (tileMap->getTileSize().width * tileMap->getMapSize().width)
#define MAP_HEIGHT (tileMap->getTileSize().height * tileMap->getMapSize().height)


#define WIN_WIDTH  (CCDirector::sharedDirector()->getWinSize().width)
#define WIN_HEIGHT  (CCDirector::sharedDirector()->getWinSize().height)


CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();


    // add layer as a child to scene
    scene->addChild(layer);


    // return the scene
    return scene;
}




// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }


tileMap = CCTMXTiledMap::create("MyTileMap.tmx");


CCTMXLayer* backLayer = tileMap->layerNamed("Tile Layer 1");


CCAssert(backLayer, "Can not find layer named by (Tile Layer 1)");


this->addChild(tileMap); // 地图加到layer上面


CCPoint anchorPos = tileMap->getAnchorPoint();
CCPoint mapPos = tileMap->getPosition();


CCTMXObjectGroup* og = tileMap->objectGroupNamed("spritePositions");
CCDictionary* posInfoDict = og->objectNamed("ninjaBirthPoint");
int x = posInfoDict->valueForKey("x")->intValue();
int y = posInfoDict->valueForKey("y")->intValue();




ninja = CCSprite::create("Player.png");
ninja->setPosition(ccp(x, y));
tileMap->addChild(ninja);


this->setMapPosForView(ccp(x, y));


this->setTouchEnabled(true); // 是layer具有响应触摸事件的能力
CCDirector::sharedDirector()->getTouchDispatcher()
->addTargetedDelegate(this, 0, true);


flagLayer = tileMap->layerNamed("flag_layer");
flagLayer->setVisible(false);




scoreBord = CCLabelTTF::create("socre:0", "Arial", 30);
scoreBord->setPosition(ccp(WIN_WIDTH - 60, 30));
this->addChild(scoreBord, 30);
this->_score = 0;


CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("TileMap.wav", true);


    return true;
}


bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
return true;
}


void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
return;
}


CCPoint HelloWorld::cocoscoord2tilemapcoord(CCPoint pos)
{
CCPoint coord;
coord.x = (int)(pos.x / GET_TILE_WIDTH); 
coord.y = (int)((MAP_WIDTH - pos.y) / GET_TILE_HEIGHT);


return coord;
}


void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
CCPoint touchPos = pTouch->getLocation();
touchPos = tileMap->convertToNodeSpace(touchPos); // 从相对于屏幕的位置转换为相对于map的位置


CCPoint origPos = ninja->getPosition(); //ninja是map的孩子,所以相对于地图的位置
CCPoint diff = touchPos - origPos;
CCPoint ninjaDiff = ccp(0, 0);


if (abs(diff.x) > abs(diff.y)){
if (diff.x > 0){
ninjaDiff.x = tileMap->getTileSize().width;
}
else
{
ninjaDiff.x = -tileMap->getTileSize().width;;
}
}
else{
if (diff.y > 0){
ninjaDiff.y = tileMap->getTileSize().height;
}
else
{
ninjaDiff.y = -tileMap->getTileSize().height;
}
}


CCPoint newPos = origPos + ninjaDiff;


CCPoint tilePos = this->cocoscoord2tilemapcoord(newPos);

int oneTileId = flagLayer->tileGIDAt(tilePos);// 根据tilemap坐标得到层上物体的id
 
if (oneTileId == 0){// 不会碰撞
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("move.wav");
ninja->setPosition(newPos);
this->setMapPosForView(newPos);
return;
}


CCDictionary* propertiesOnOneTile = tileMap->propertiesForGID(oneTileId);
const CCString* collide = propertiesOnOneTile->valueForKey("Collide");
const CCString* fruit = propertiesOnOneTile->valueForKey("fruit");


//判断如果新位置是碰撞属性true,不可以移动
if (collide && collide->compare("true") == 0) //碰撞
{
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("hit.wav");
; // 不移动
}
else if (fruit && fruit->compare("true") == 0) //是一个西瓜
{
CCTMXLayer* fruitLayer = tileMap->layerNamed("foreground");
fruitLayer->removeTileAt(tilePos); // 吃掉西瓜 得分
flagLayer->removeTileAt(tilePos); // 移除障碍


CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pickup.wav");


this->_score ++;
char str[40];
sprintf(str, "score:%d", _score);
scoreBord->setString(str);


}
else
{
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("move.wav");
ninja->setPosition(newPos);
this->setMapPosForView(newPos);
}


return;
}


void HelloWorld::setMapPosForView(cocos2d::CCPoint playerPos){


CCPoint orig = playerPos;
CCPoint dest = ccp(WIN_WIDTH / 2, WIN_HEIGHT / 2);
CCPoint distance = ccpSub(dest, orig);
CCPoint newMapPos = ccp(0, 0) + distance;


newMapPos.x = (newMapPos.x > 0? 0:newMapPos.x);
newMapPos.y = (newMapPos.y > 0? 0:newMapPos.y);


newMapPos.x = (newMapPos.x < WIN_WIDTH - MAP_WIDTH? 
WIN_WIDTH-MAP_WIDTH:newMapPos.x);
newMapPos.y = (newMapPos.y < WIN_HEIGHT - MAP_HEIGHT? 
WIN_HEIGHT - MAP_HEIGHT:newMapPos.y);


tileMap->setPosition(newMapPos);


}




void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
#endif
}

这篇关于ccp之间是不可以直接进行+,-的,要用ccpSub和ccpAdd。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1089979

相关文章

Java对象和JSON字符串之间的转换方法(全网最清晰)

《Java对象和JSON字符串之间的转换方法(全网最清晰)》:本文主要介绍如何在Java中使用Jackson库将对象转换为JSON字符串,并提供了一个简单的工具类示例,该工具类支持基本的转换功能,... 目录前言1. 引入 Jackson 依赖2. 创建 jsON 工具类3. 使用示例转换 Java 对象为

Python进行PDF文件拆分的示例详解

《Python进行PDF文件拆分的示例详解》在日常生活中,我们常常会遇到大型的PDF文件,难以发送,将PDF拆分成多个小文件是一个实用的解决方案,下面我们就来看看如何使用Python实现PDF文件拆分... 目录使用工具将PDF按页数拆分将PDF的每一页拆分为单独的文件将PDF按指定页数拆分根据页码范围拆分

Linux使用cut进行文本提取的操作方法

《Linux使用cut进行文本提取的操作方法》Linux中的cut命令是一个命令行实用程序,用于从文件或标准输入中提取文本行的部分,本文给大家介绍了Linux使用cut进行文本提取的操作方法,文中有详... 目录简介基础语法常用选项范围选择示例用法-f:字段选择-d:分隔符-c:字符选择-b:字节选择--c

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

java父子线程之间实现共享传递数据

《java父子线程之间实现共享传递数据》本文介绍了Java中父子线程间共享传递数据的几种方法,包括ThreadLocal变量、并发集合和内存队列或消息队列,并提醒注意并发安全问题... 目录通过 ThreadLocal 变量共享数据通过并发集合共享数据通过内存队列或消息队列共享数据注意并发安全问题总结在 J

Python利用PIL进行图片压缩

《Python利用PIL进行图片压缩》有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所以本文为大家介绍了Python中图片压缩的方法,需要的可以参考下... 有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所有可以对文件中的图