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

触屏响应

//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));


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值