//helloworld.cpp
#include "HelloWorldScene.h"
#include"player.h"
#define PI 3.1415926
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("player.plist", "player.png");
auto sprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("0_run_0.png"));
sprite->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
sprite->setTag(123);
this->addChild(sprite);
moveBy = nullptr;
auto listener = EventListenerTouchOneByOne::create();//創建一個觸摸監聽(單點觸摸)
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);//指定觸摸的回呼函式
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
m_bIsRunning = false;
return true;
}
bool HelloWorld::onTouchBegan(Touch *pTouch, Event *pEvent)
{
m_pTouchPoint = pTouch->getLocation();
return true;//回傳true表示接收觸摸事件
}
void HelloWorld::onTouchMoved(Touch* pTouch, Event *event)
{
if (m_bIsRunning == false && sqrt((pTouch->getDelta().x*pTouch->getDelta().x) + (pTouch->getDelta().y*pTouch->getDelta().y)) > 18)
{
auto sp = (Sprite*)this->getChildByTag(123);
m_bIsRunning = true;
//一象限
if (pTouch->getLocation().x > m_pTouchPoint.x&&pTouch->getLocation().y > m_pTouchPoint.y)
{
if ((pTouch->getDelta().y) / (pTouch->getDelta().x) < tan(PI / 8))
{
if (sp->isFlippedX() == false)
{
sp->setFlippedX(true);
}
sp->runAction(Player::getInstance()->runleft);
}
else if ((pTouch->getDelta().y) / (pTouch->getDelta().x) > tan(PI / 8) && ((pTouch->getDelta().y) / (pTouch->getDelta().x)< tan(0.75*PI)))
{
if (sp->isFlippedX() == false)
{
sp->setFlippedX(true);
}
sp->runAction(Player::getInstance()->runleftup);
}
else
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runup);
}
}
////二像限
if (pTouch->getLocation().x < m_pTouchPoint.x&&pTouch->getLocation().y > m_pTouchPoint.y)
{
if ((pTouch->getDelta().y) / (pTouch->getDelta().x)< tan(PI / 8))
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runleft);
}
else if (((pTouch->getDelta().y) / (pTouch->getDelta().x) > tan(PI / 8)) && (abs((pTouch->getDelta().y) / (pTouch->getDelta().x)) < tan(0.75*PI)))
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runleftup);
}
else
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runup);
}
}
////三像限
if (pTouch->getLocation().x < m_pTouchPoint.x&&pTouch->getLocation().y < m_pTouchPoint.y)
{
if ((pTouch->getDelta().y) / (pTouch->getDelta().x) < tan(PI / 8))
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runleft);
}
else if (((pTouch->getDelta().y) / (pTouch->getDelta().x)> tan(PI / 8)) && ((pTouch->getDelta().y) / (pTouch->getDelta().x) < tan(0.75*PI)))
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runleftdown);
}
else
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->rundown);
} //m_pSp->runAction(g_AnimateM[0]->rundown);
}
////四像限
if (pTouch->getLocation().x > m_pTouchPoint.x&&pTouch->getLocation().y < m_pTouchPoint.y)
{
if ((pTouch->getDelta().y) / (pTouch->getDelta().x) < tan(PI / 8))
{
if (sp->isFlippedX() == false)
{
sp->setFlippedX(true);
}
sp->runAction(Player::getInstance()->runleft);
}
else if (((pTouch->getDelta().y) / (pTouch->getDelta().x) > tan(PI / 8)) && (abs((pTouch->getDelta().y) / (pTouch->getDelta().x)) < tan(0.75*PI)))
{
if (sp->isFlippedX() == false)
{
sp->setFlippedX(true);
}
sp->runAction(Player::getInstance()->runleftdown);
}
else
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->rundown);
}
}
}
}
void HelloWorld::onTouchEnded(Touch* pTouch, Event *event)
{
m_bIsRunning = false;
auto sp= (Sprite*)this->getChildByTag(123);
sp->stopAllActions();
}
uj5u.com熱心網友回復:
//player.cpp#include "player.h"
static Player *player = nullptr;
Player * Player::getInstance()
{
if (player ==nullptr)
{
player = new Player();
player->init();
}
return player;
}
Player::Player()
{
standdown = nullptr;
standleftdown = nullptr;
standleft=nullptr;
standleftup = nullptr;
standup = nullptr;
//run
rundown = nullptr;
runleftdown = nullptr;
runleft = nullptr;
runleftup = nullptr;
runup = nullptr;
//attack
attackdown = nullptr;
attackleftdown = nullptr;
attackleft = nullptr;
attackleftup = nullptr;
attackup = nullptr;
//beattack
beattackdown = nullptr;
beattackleftdown = nullptr;
beattackleft = nullptr;
beattackleftup = nullptr;
beattackup = nullptr;
//death
deathdown = nullptr;
deathleftdown = nullptr;
deathleft = nullptr;
deathleftup = nullptr;
deathup = nullptr;
//animation
//stand
animationstanddown = nullptr;
animationstandleftdown = nullptr;
animationstandleft = nullptr;
animationstandleftup = nullptr;
animationstandup = nullptr;
//run
animationrundown = nullptr;
animationrunleftdown = nullptr;
animationrunleft = nullptr;
animationrunleftup = nullptr;
animationrunup = nullptr;
//attack
animationattackdown = nullptr;
animationattackleftdown = nullptr;
animationattackleft = nullptr;
animationattackleftup = nullptr;
animationattackup = nullptr;
//beattack
animationbeattackdown = nullptr;
animationbeattackleftdown = nullptr;
animationbeattackleft = nullptr;
animationbeattackleftup = nullptr;
animationbeattackup = nullptr;
//death
animationdeathdown = nullptr;
animationdeathleftdown = nullptr;
animationdeathleft = nullptr;
animationdeathleftup = nullptr;
animationdeathup = nullptr;
}
Player::~Player()
{
rundown ->release();
runleftdown->release();
runleft->release();
runleftup->release();
runup->release();
animationrundown->release();
animationrunleftdown->release();
animationrunleft->release();
animationrunleftup->release();
animationrunup->release();
player->release();
}
bool Player::init()
{
initRun();
initStand();
initAttack();
initBeattack();
initDeath();
return true;
}
bool Player::initRun()
{
char name[256] = { 0 };
//rundown
animationrundown = Animation::create();
animationrundown->retain();
for (int i = 0; i < 6; i++)
{
sprintf(name, "0_run_%d.png", i);
animationrundown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationrundown->setDelayPerUnit(0.15);
animationrundown->setRestoreOriginalFrame(true);
animationrundown->setLoops(-1);
rundown=Animate::create(animationrundown);
//runleftdown
animationrunleftdown=Animation::create();
animationrunleftdown ->retain();
for (int i = 0; i < 6; i++)
{
sprintf(name, "1_run_%d.png", i);
animationrunleftdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationrunleftdown->setDelayPerUnit(0.15);
animationrunleftdown->setRestoreOriginalFrame(true);
animationrunleftdown->setLoops(-1);
runleftdown = Animate::create(animationrunleftdown);
//runleft
animationrunleft = Animation::create();
animationrunleft->retain();
for (int i = 0; i < 6; i++)
{
sprintf(name, "2_run_%d.png", i);
animationrunleft->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationrunleft->setDelayPerUnit(0.15);
animationrunleft->setRestoreOriginalFrame(true);
animationrunleft->setLoops(-1);
runleft = Animate::create(animationrunleft);
runleft->retain();
//runleftup
animationrunleftup = Animation::create();
animationrunleftup->retain();
for (int i = 0; i < 6; i++)
{
sprintf(name, "3_run_%d.png", i);
animationrunleftup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationrunleftup->setDelayPerUnit(0.15);
animationrunleftup->setRestoreOriginalFrame(true);
animationrunleftup->setLoops(-1);
runleftup = Animate::create(animationrunleftup);
runleftup->retain();
//runup
animationrunup = Animation::create();
animationrunup->retain();
for (int i = 0; i < 6; i++)
{
sprintf(name, "4_run_%d.png", i);
animationrunup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationrunup->setDelayPerUnit(0.15);
animationrunup->setRestoreOriginalFrame(true);
animationrunup->setLoops(-1);
runup = Animate::create(animationrunup);
runup->retain();
return true;
}
uj5u.com熱心網友回復:
bool Player::initStand(){
char name[256] = { 0 };
//standdown
animationstanddown = Animation::create();
for (int i = 0; i < 2; i++)
{
sprintf(name, "0_stand_%d.png", i);
animationstanddown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationstanddown->setDelayPerUnit(0.15);
animationstanddown->setRestoreOriginalFrame(true);
animationstanddown->setLoops(-1);
standdown = Animate::create(animationstanddown);
//standleftdown
animationstandleftdown = Animation::create();
for (int i = 0; i < 2; i++)
{
sprintf(name, "1_stand_%d.png", i);
animationstandleftdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationstandleftdown->setDelayPerUnit(0.15);
animationstandleftdown->setRestoreOriginalFrame(true);
animationstandleftdown->setLoops(-1);
standleftdown = Animate::create(animationstandleftdown);
//standleft
animationstandleft = Animation::create();
for (int i = 0; i < 2; i++)
{
sprintf(name, "2_stand_%d.png", i);
animationstandleft->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationstandleft->setDelayPerUnit(0.15);
animationstandleft->setRestoreOriginalFrame(true);
animationstandleft->setLoops(-1);
standleft = Animate::create(animationstandleft);
//standleftup
animationstandleftup = Animation::create();
for (int i = 0; i < 2; i++)
{
sprintf(name, "3_stand_%d.png", i);
animationstandleftup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationstandleftup->setDelayPerUnit(0.15);
animationstandleftup->setRestoreOriginalFrame(true);
animationstandleftup->setLoops(-1);
standleftup = Animate::create(animationstandleftup);
//standup
animationstandup = Animation::create();
for (int i = 0; i < 2; i++)
{
sprintf(name, "4_stand_%d.png", i);
animationstandup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationstandup->setDelayPerUnit(0.15);
animationstandup->setRestoreOriginalFrame(true);
animationstandup->setLoops(-1);
standup->create(animationstandup);
return true;
}
bool Player::initAttack()
{
char name[256] = { 0 };
//attackdown
animationattackdown = Animation::create();
animationattackdown->retain();
for (int i = 0; i < 5; i++)
{
sprintf(name, "0_attack_%d.png", i);
animationattackdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationattackdown->setDelayPerUnit(0.15);
animationattackdown->setRestoreOriginalFrame(true);
animationattackdown->setLoops(-1);
attackdown = Animate::create(animationattackdown);
//attackleftdown
animationattackleftdown = Animation::create();
for (int i = 0; i < 5; i++)
{
sprintf(name, "1_attack_%d.png", i);
animationattackleftdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationattackleftdown->setDelayPerUnit(0.15);
animationattackleftdown->setRestoreOriginalFrame(true);
animationattackleftdown->setLoops(-1);
attackleftdown = Animate::create(animationattackleftdown);
//attackleft
animationattackleft = Animation::create();
for (int i = 0; i < 5; i++)
{
sprintf(name, "2_attack_%d.png", i);
animationattackleft->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationattackleft->setDelayPerUnit(0.15);
animationattackleft->setRestoreOriginalFrame(true);
animationattackleft->setLoops(-1);
attackleft = Animate::create(animationattackleft);
//attackleftup
animationattackleftup = Animation::create();
for (int i = 0; i < 5; i++)
{
sprintf(name, "3_attack_%d.png", i);
animationattackleftup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationattackleftup->setDelayPerUnit(0.15);
animationattackleftup->setRestoreOriginalFrame(true);
animationattackleftup->setLoops(-1);
attackleftup = Animate::create(animationattackleftup);
//attackup
animationattackup = Animation::create();
for (int i = 0; i < 5; i++)
{
sprintf(name, "4_attack_%d.png", i);
animationattackup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
}
animationattackup->setDelayPerUnit(0.15);
animationattackup->setRestoreOriginalFrame(true);
animationattackup->setLoops(-1);
attackup = Animate::create(animationattackup);
return true;
}
bool Player::initBeattack()
{
char name[256] = { 0 };
//beattackdown
animationbeattackdown = Animation::create();
sprintf(name, "0_beattack_0.png");
animationbeattackdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
animationbeattackdown->setDelayPerUnit(0.15);
animationbeattackdown->setRestoreOriginalFrame(true);
animationbeattackdown->setLoops(-1);
beattackdown = Animate::create(animationbeattackdown);
//beattackleftdown
animationbeattackleftdown = Animation::create();
sprintf(name, "1_beattack_0.png");
animationbeattackleftdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
animationbeattackleftdown->setDelayPerUnit(0.15);
animationbeattackleftdown->setRestoreOriginalFrame(true);
animationbeattackleftdown->setLoops(-1);
beattackleftdown = Animate::create(animationbeattackleftdown);
//beattackleft
animationbeattackleft = Animation::create();
sprintf(name, "2_beattack_0.png");
animationbeattackleft->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
animationbeattackleft->setDelayPerUnit(0.15);
animationbeattackleft->setRestoreOriginalFrame(true);
animationbeattackleft->setLoops(-1);
beattackleft = Animate::create(animationbeattackleft);
//beattackleftup
animationbeattackleftup = Animation::create();
sprintf(name, "3_beattack_0.png");
animationbeattackleftup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
animationbeattackleftup->setDelayPerUnit(0.15);
animationbeattackleftup->setRestoreOriginalFrame(true);
animationbeattackleftup->setLoops(-1);
beattackleftup = Animate::create(animationbeattackleftup);
//beattackup
animationbeattackup = Animation::create();
sprintf(name, "4_beattack_0.png");
animationbeattackup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
animationbeattackup->setDelayPerUnit(0.15);
animationbeattackup->setRestoreOriginalFrame(true);
animationbeattackup->setLoops(-1);
beattackup = Animate::create(animationbeattackup);
return true;
}
bool Player::initDeath()
{
char name[256] = { 0 };
//deathdown
animationdeathdown = Animation::create();
sprintf(name, "0_death_0.png");
animationdeathdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
animationdeathdown->setDelayPerUnit(0.15);
animationdeathdown->setRestoreOriginalFrame(true);
animationdeathdown->setLoops(-1);
deathdown = Animate::create(animationdeathdown);
//deathleftdown
animationdeathleftdown = Animation::create();
sprintf(name, "1_death_0.png");
animationdeathleftdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
animationdeathleftdown->setDelayPerUnit(0.15);
animationdeathleftdown->setRestoreOriginalFrame(true);
animationdeathleftdown->setLoops(-1);
deathleftdown = Animate::create(animationdeathleftdown);
//deathleft
animationdeathleft = Animation::create();
sprintf(name, "2_death_0.png");
animationdeathleft->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
animationdeathleft->setDelayPerUnit(0.15);
animationdeathleft->setRestoreOriginalFrame(true);
animationdeathleft->setLoops(-1);
deathleft = Animate::create(animationdeathleft);
//deathleftup
animationdeathleftup = Animation::create();
sprintf(name, "3_death_0.png");
animationdeathleftup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
animationdeathleftup->setDelayPerUnit(0.15);
animationdeathleftup->setRestoreOriginalFrame(true);
animationdeathleftup->setLoops(-1);
deathleftup = Animate::create(animationdeathleftup);
//deathup
animationdeathup = Animation::create();
sprintf(name, "4_death_0.png");
animationdeathup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name));
animationdeathup->setDelayPerUnit(0.15);
animationdeathup->setRestoreOriginalFrame(true);
animationdeathup->setLoops(-1);
deathup = Animate::create(animationdeathup);
return true;
}
uj5u.com熱心網友回復:
程式的功能就是通過touchmove得到行走方向,touchend停止動作。不知道怎么回事,運行時觸摸點多移動幾次就會報錯。跟宗錯誤顯示斷在runaction()里面的addaction里面呼叫的ccArrayAppendObject(element->actions, action),arr->arr 就是引數element->actions
引發了例外: 寫入訪問權限沖突。
arr->arr 是 0x1110112。
uj5u.com熱心網友回復:

是這個錯誤嗎?
重復運行相同動作了,使用 sprite->stopAllActions() 移除原本存在的動作,然后在運行就沒有問題了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/70352.html
標籤:Cocos2d-x
