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

相关文章

python进行while遍历的常见错误解析

《python进行while遍历的常见错误解析》在Python中选择合适的遍历方式需要综合考虑可读性、性能和具体需求,本文就来和大家讲解一下python中while遍历常见错误以及所有遍历方法的优缺点... 目录一、超出数组范围问题分析错误复现解决方法关键区别二、continue使用问题分析正确写法关键点三

Python对PDF书签进行添加,修改提取和删除操作

《Python对PDF书签进行添加,修改提取和删除操作》PDF书签是PDF文件中的导航工具,通常包含一个标题和一个跳转位置,本教程将详细介绍如何使用Python对PDF文件中的书签进行操作... 目录简介使用工具python 向 PDF 添加书签添加书签添加嵌套书签Python 修改 PDF 书签Pytho

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

Pandas进行周期与时间戳转换的方法

《Pandas进行周期与时间戳转换的方法》本教程将深入讲解如何在pandas中使用to_period()和to_timestamp()方法,完成时间戳与周期之间的转换,并结合实际应用场景展示这些方法的... 目录to_period() 时间戳转周期基本操作应用示例to_timestamp() 周期转时间戳基

Java使用Stream流的Lambda语法进行List转Map的操作方式

《Java使用Stream流的Lambda语法进行List转Map的操作方式》:本文主要介绍Java使用Stream流的Lambda语法进行List转Map的操作方式,具有很好的参考价值,希望对大... 目录背景Stream流的Lambda语法应用实例1、定义要操作的UserDto2、ListChina编程转成M

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too

Pandas使用AdaBoost进行分类的实现

《Pandas使用AdaBoost进行分类的实现》Pandas和AdaBoost分类算法,可以高效地进行数据预处理和分类任务,本文主要介绍了Pandas使用AdaBoost进行分类的实现,具有一定的参... 目录什么是 AdaBoost?使用 AdaBoost 的步骤安装必要的库步骤一:数据准备步骤二:模型

使用Pandas进行均值填充的实现

《使用Pandas进行均值填充的实现》缺失数据(NaN值)是一个常见的问题,我们可以通过多种方法来处理缺失数据,其中一种常用的方法是均值填充,本文主要介绍了使用Pandas进行均值填充的实现,感兴趣的... 目录什么是均值填充?为什么选择均值填充?均值填充的步骤实际代码示例总结在数据分析和处理过程中,缺失数

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

通过Spring层面进行事务回滚的实现

《通过Spring层面进行事务回滚的实现》本文主要介绍了通过Spring层面进行事务回滚的实现,包括声明式事务和编程式事务,具有一定的参考价值,感兴趣的可以了解一下... 目录声明式事务回滚:1. 基础注解配置2. 指定回滚异常类型3. ​不回滚特殊场景编程式事务回滚:1. ​使用 TransactionT