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

相关文章

大语言模型(LLMs)能够进行推理和规划吗?

大语言模型(LLMs),基本上是经过强化训练的 n-gram 模型,它们在网络规模的语言语料库(实际上,可以说是我们文明的知识库)上进行了训练,展现出了一种超乎预期的语言行为,引发了我们的广泛关注。从训练和操作的角度来看,LLMs 可以被认为是一种巨大的、非真实的记忆库,相当于为我们所有人提供了一个外部的系统 1(见图 1)。然而,它们表面上的多功能性让许多研究者好奇,这些模型是否也能在通常需要系

Python应用开发——30天学习Streamlit Python包进行APP的构建(9)

st.area_chart 显示区域图。 这是围绕 st.altair_chart 的语法糖。主要区别在于该命令使用数据自身的列和指数来计算图表的 Altair 规格。因此,在许多 "只需绘制此图 "的情况下,该命令更易于使用,但可定制性较差。 如果 st.area_chart 无法正确猜测数据规格,请尝试使用 st.altair_chart 指定所需的图表。 Function signa

气象站的种类和应用范围可以根据不同的分类标准进行详细的划分和描述

气象站的种类和应用范围可以根据不同的分类标准进行详细的划分和描述。以下是从不同角度对气象站的种类和应用范围的介绍: 一、气象站的种类 根据用途和安装环境分类: 农业气象站:专为农业生产服务,监测土壤温度、湿度等参数,为农业生产提供科学依据。交通气象站:用于公路、铁路、机场等交通场所的气象监测,提供实时气象数据以支持交通运营和调度。林业气象站:监测林区风速、湿度、温度等气象要素,为林区保护和

企业如何进行员工的网络安全意识培训?

企业网络安全意识培训的重要性         企业网络安全意识培训是提升员工网络安全素质的关键环节。随着网络技术的快速发展,企业面临的网络安全威胁日益增多,员工的网络安全意识和技能水平直接关系到企业的信息安全和业务连续性。因此,企业需要通过系统的网络安全意识培训,提高员工对网络安全的认识和防范能力,从而降低企业在面对潜在安全风险时的损失和影响。 企业网络安全意识培训的方法         企

使用JWT进行安全通信

在现代Web应用中,安全通信是至关重要的。JSON Web Token(JWT)是一种流行的安全通信方式,它允许用户和服务器之间安全地传输信息。JWT是一种紧凑的、URL安全的表示方法,用于在两方之间传输信息。本文将详细介绍JWT的工作原理,并提供代码示例帮助新人理解和实现JWT。 什么是JWT? JWT是一种开放标准(RFC 7519),它定义了一种紧凑且自包含的方式,用于在各方之间以JSO

把Tiled中做出的地图弄到项目中~~就是懒,为了以后直接复制写过来

1.现在.h中声明private: cocos2d::CCSprite* ninja; cocos2d::CCTMXTiledMap*  tileMap; 然后.cpp中加入tileMap = CCTMXTiledMap::create("MyTileMap.tmx"); CCTMXLayer* backLayer = tileMap->layerNamed("Tile L

直接得到Json串,转换为字典

0.新创建一个json文件,把json串拷贝到里面 1.先通过MainBundle找到资源对应的路径 2.将文件转换为NSData 3.通过NSJSonSerization得到字典 NSString*fileName=[[NSBundle mainBundle] pathForResource:@"myJson" ofType:@"json"];           NS

1_CString char* string之间的关系

CString转char*,string string转char*,CString char* 转CString,string 一、CString转char*,string //字串转换测试 CString CString1; std::string string1; CHAR* char1=NULL; //1string1=CString1.GetBuffer();CStri

【Linux文件系统】被打开的文件与文件系统的文件之间的关联刨析总结

操作系统管理物理内存以及与外设磁盘硬件进行数据的交换 操作系统如何管理物理内存呢? 其实操作系统内核先对内存先描述再组织的!操作系统管理内存的基本单位是4KB,操作系统会为每一个4KB大小的物理内存块创建一个描述该4KB内存块的struct page结构体,该结构体存储着这4KB内存块的属性信息,通过管理struct page来对内存进行管理,page结构体的大小比较小,OS通常将它们组成一个

使用 GoPhish 和 DigitalOcean 进行网络钓鱼

配置环境 数字海洋VPS 我创建的丢弃物被分配了一个 IP 地址68.183.113.176 让我们登录VPS并安装邮件传递代理: ssh root@68.183.113.176apt-get install postfix 后缀配置中的点变量到我们在 DigitalOcean 中分配的 IP:mynetworks nano /etc/postfix/main.cf