欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

cocos2d x 触屏响应、序列帧动画

程序员文章站 2024-03-26 00:00:10
...

触屏响应

//add touch listener
	EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
	listener->setSwallowTouches(true);
	listener->onTouchBegan = CC_CALLBACK_2(GameSence::onTouchBegan, this);
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

添加监听器,此段在init()里面写,CC_CALL_BACK_2回调函数


bool GameSence::onTouchBegan(Touch *touch, Event *unused_event) {

	auto location = touch->getLocation();

	auto cheese = Sprite::create("cheese.png");
	cheese->setPosition(Vec2(location.x, location.y));
	this->addChild(cheese, 1);
	auto fadeOut = FadeOut::create(6);
	cheese->runAction(fadeOut);

	auto mouseMoveToCheese = MoveTo::create(1.5, Vec2(location.x, location.y));
	mouse->runAction(mouseMoveToCheese);

	return true;
}

我的ontouchbegin()即触屏相应出发的函数是得到当前点击屏幕的位置,然后在该位置放一个“奶酪”精灵,并且慢慢消失,然后老鼠一栋到该位置。

里面的函数可以随便写~

序列帧动画:

第一种:从plist里面加载:

AppDelegate.cpp中预先加载动画资源
// load game resource
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("general-sheet.plist");
	char totalFrames = 3;
	char frameName[20];
	Animation* legAnimation = Animation::create();

	for (int i = 0; i < totalFrames; i++)
	{
		sprintf(frameName, "miner-leg-%d.png", i);
		legAnimation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(frameName));
	}
	legAnimation->setDelayPerUnit(0.1);
	AnimationCache::getInstance()->addAnimation(legAnimation, "legAnimation");

在plist对应的png里面找到想要的图片(plist可以在vs里面打开获得响应图片的信息(名字)),然后上面是一种方法,直接找到3副名为miner-leg-号码的帧,然后创建animation

auto leg = Sprite::createWithSpriteFrameName("miner-leg-0.png");
	Animate* legAnimate = Animate::create(AnimationCache::getInstance()->getAnimation("legAnimation"));
	leg->runAction(RepeatForever::create(legAnimate));
	leg->setPosition(110 + origin.x, origin.y + 102);
	this->addChild(leg, 1);

然后在scence.cpp里面就可以运动animation做动画了,repeatforever是一直动下去,可以运用repeat函数


还有另一种找帧的方法,就是直接根据名字找:

Animation* diamondAnimation = Animation::create();
	diamondAnimation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("diamond-0.png"));
	diamondAnimation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("pulled-diamond-2.png"));
	diamondAnimation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("diamond-2.png"));
	diamondAnimation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("pulled-diamond-4.png"));
	diamondAnimation->setDelayPerUnit(0.1);
	AnimationCache::getInstance()->addAnimation(diamondAnimation, "diamondAnimation");


第二种:createWithTexture(在png里面加载):

// 运动动画(帧数:8帧,高:101,宽:68)
	auto texture3 = Director::getInstance()->getTextureCache()->addImage("$lucia_forward.png");
	run.reserve(9);
	for (int i = 0; i < 8; i++) {
		auto frame = SpriteFrame::createWithTexture(texture3, CC_RECT_PIXELS_TO_POINTS(Rect(68 * i, 0, 68, 101)));
		run.pushBack(frame);
	}
	run.pushBack(SpriteFrame::createWithTexture(texture3, CC_RECT_PIXELS_TO_POINTS(Rect(0, 0, 68, 101))));
然后可以使用动画:
float x = (player->getPosition().x - 20 > origin.x + player->getContentSize().width / 2) ? player->getPosition().x - 20 : origin.x + player->getContentSize().width / 2;
	auto movetoA = MoveTo::create(0.8, Vec2(x, player->getPosition().y));
	auto actionRun = Animation::createWithSpriteFrames(run, 0.1f);
	auto spawn = Spawn::createWithTwoActions(movetoA, Repeat::create(Animate::create(actionRun), 1));
	player->runAction(spawn);
关键在于转换成animation,而后转换成animate,然后可以直接player->runAction(Repeat::create(Animate::create(actionRun),1));