Follow and unfollow sprite

Follow and unfollow sprite
0

I am trying to follow and unfollow sprite by using this->runAction(action) and this->stopAction(action)

this error thrown (but If i use run-stop actions out of schedule scope it works):

Exception thrown: read access violation.
action->**** was 0xDDDDDDDD.

virtual studio focused this file CCActionManager.cpp line 193 :
action->startWithTarget(target);

function is
void ActionManager::addAction(...)

my code:
in header:

cocos2d::Action * followTheSprite;

HelloWorld::init :

    followTheSprite = Follow::create(sprite3, Rect(0, 0, 0, 0));
    Director::getInstance()->getScheduler()->schedule([&](float dt){

        // doesnt work here, but this line works out of schedule scope,
    	this->runAction(followTheSprite);

    	}, this, 1.5f, false, "schedulerKey");
  • I use schedule because I need to check sprite whether is stopped or not

but followTheSprite isn’t an Action, it’s a Sprite

please check this line, followTheSprite is Action type pointer:
followTheSprite = Follow::create(sprite3, Rect(0, 0, 0, 0));

show more code, please.

Why Rect(0, 0, 0, 0)? make it easier, try with:
scene->runAction(Follow::create(sprite));

where scene is the current scene, and sprite is the sprite to follow.
And if you want to unfollow the sprite, just call stopAction

	Director::getInstance()->getScheduler()->schedule([&](float dt){
		
	if(this->getNumberOfRunningActions() > 0){
		this->stopAction(Follow::create(sprite3));	
	}else{
		this->runAction(Follow::create(sprite3));	
	}
		
	}, this, 3.5f, false, "schedulerKey");

I have no other actions , getNumberOfRunningActions() is 0 at first and next schedule call it becomes 1 and never turns 0 on next schecules.

but it should become 0 again on next schedule by calling stopAction(…)

my all code is this,in physics scene have just physics sprite, scheduler

I can create test project for this but I will copy unrelated default codes here

I fixed by assigning returned Action to action pointer

followTheSprite = this->runAction(Follow::create(sprite3));

to stop actions is same : this->stopAction(followTheSprite);

So you fixed it with the same logic that I said. However, you marked your post as solution :thinking:
Hahaha. Good for you. :+1:

Hi @tranthor thank you for valuable reply but I didnt fixed issue with your reply but it helped me to dig more different. here is my actual schedule, please check.

Director::getInstance()->getScheduler()->schedule([&](float dt){
	sprite3action = Follow::create(sprite3, Rect(0,0, 0, 0));
	auto absX = abs(sprite3->getPhysicsBody()->getVelocity().x);
	auto absY = abs(sprite3->getPhysicsBody()->getVelocity().y);
	
		if(absX > 1 && absY > 1){
			if(this->getNumberOfRunningActions() > 0){
				this->stopAction(followTheSprite);
			}
			
		}else{
			if(this->getNumberOfRunningActions() == 0){
				followTheSprite = this->runAction(sprite3action);	
			}
			
		}
	CCLOG("Actions %d", this->getNumberOfRunningActions());	
	
}, this, 1.5f, false, "schedulerKey");

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.