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

相关文章

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令

MySQL按时间维度对亿级数据表进行平滑分表

《MySQL按时间维度对亿级数据表进行平滑分表》本文将以一个真实的4亿数据表分表案例为基础,详细介绍如何在不影响线上业务的情况下,完成按时间维度分表的完整过程,感兴趣的小伙伴可以了解一下... 目录引言一、为什么我们需要分表1.1 单表数据量过大的问题1.2 分表方案选型二、分表前的准备工作2.1 数据评估

MySQL进行分片合并的实现步骤

《MySQL进行分片合并的实现步骤》分片合并是指在分布式数据库系统中,将不同分片上的查询结果进行整合,以获得完整的查询结果,下面就来具体介绍一下,感兴趣的可以了解一下... 目录环境准备项目依赖数据源配置分片上下文分片查询和合并代码实现1. 查询单条记录2. 跨分片查询和合并测试结论分片合并(Shardin

Java中数组与栈和堆之间的关系说明

《Java中数组与栈和堆之间的关系说明》文章讲解了Java数组的初始化方式、内存存储机制、引用传递特性及遍历、排序、拷贝技巧,强调引用数据类型方法调用时形参可能修改实参,但需注意引用指向单一对象的特性... 目录Java中数组与栈和堆的关系遍历数组接下来是一些编程小技巧总结Java中数组与栈和堆的关系关于

SpringBoot结合Knife4j进行API分组授权管理配置详解

《SpringBoot结合Knife4j进行API分组授权管理配置详解》在现代的微服务架构中,API文档和授权管理是不可或缺的一部分,本文将介绍如何在SpringBoot应用中集成Knife4j,并进... 目录环境准备配置 Swagger配置 Swagger OpenAPI自定义 Swagger UI 底

基于Python Playwright进行前端性能测试的脚本实现

《基于PythonPlaywright进行前端性能测试的脚本实现》在当今Web应用开发中,性能优化是提升用户体验的关键因素之一,本文将介绍如何使用Playwright构建一个自动化性能测试工具,希望... 目录引言工具概述整体架构核心实现解析1. 浏览器初始化2. 性能数据收集3. 资源分析4. 关键性能指

Nginx进行平滑升级的实战指南(不中断服务版本更新)

《Nginx进行平滑升级的实战指南(不中断服务版本更新)》Nginx的平滑升级(也称为热升级)是一种在不停止服务的情况下更新Nginx版本或添加模块的方法,这种升级方式确保了服务的高可用性,避免了因升... 目录一.下载并编译新版Nginx1.下载解压2.编译二.替换可执行文件,并平滑升级1.替换可执行文件

在Java中实现线程之间的数据共享的几种方式总结

《在Java中实现线程之间的数据共享的几种方式总结》在Java中实现线程间数据共享是并发编程的核心需求,但需要谨慎处理同步问题以避免竞态条件,本文通过代码示例给大家介绍了几种主要实现方式及其最佳实践,... 目录1. 共享变量与同步机制2. 轻量级通信机制3. 线程安全容器4. 线程局部变量(ThreadL

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON:

一文解密Python进行监控进程的黑科技

《一文解密Python进行监控进程的黑科技》在计算机系统管理和应用性能优化中,监控进程的CPU、内存和IO使用率是非常重要的任务,下面我们就来讲讲如何Python写一个简单使用的监控进程的工具吧... 目录准备工作监控CPU使用率监控内存使用率监控IO使用率小工具代码整合在计算机系统管理和应用性能优化中,监