How to get position of sprite on visible rect

How to get position of sprite on visible rect
0

Suppose I have sprite and i set its position randomly, how can determine sprite is visible on screen or not and if visible get relative location on screen.

I am asking this to make mario style follow.
Follower shouldn’t follow sprite when its on screen center-200 between screen center+200

I tried Follow::createWithOffset looks like doesnt work intended or I missing something.

for example
Follow::createWithOffset(sprite3, 200, 0, Rect(0,-3200, 4000, 800));

this works intended follows sprite at xOffset 200, if I only move player right to left its still follow player at same xOffset 200 point.

any suggestion about usage of createWithOffset?
thank you

Are you using the API-Ref and the rest of our documentation? What about cpp-tests?

I read API-Ref but didnt find actual implementation about this, only parameter explanations.

I checked cpp-test topics again but I didnt find any Follow related example.

after reading source (https://github.com/cocos2d/cocos2d-x/blob/v4/cocos/2d/CCAction.cpp) rect was world rect instead no-follow area so i changed my implementation but still doesnt work intended.

Why aren’t you using setVisible and isVisible and checking the position to see if it’s on screen or off?

looks like isVisible related to its hidden property instead location, I need to know sprite screen position x,y.

I made my approach its working but dont know it hurts performance, maybe best option is createWithOffset() but there is no documentation. :slightly_frowning_face:

So getPosition()??

What you want to do is a few lines of code but you aren’t showing enough of what you are doing.

		auto sprite3nodeSpace = convertToNodeSpace(sprite3->getPosition());
	
		if( sprite3nodeSpace.x > *lastFollowpos + *offsetsize){
			*lastFollowpos = sprite3nodeSpace.x - *offsetsize;
			if(numberOfRunningActions()==0){
				sprite3action = Follow::createWithOffset(sprite3, *offsetsize, 300, Rect(0,0, 5200, 800));
				followTheSprite = this->runAction(sprite3action);	
			}	
		}
		
		if( sprite3nodeSpace.x < *lastFollowpos - *offsetsize){
			*lastFollowpos = sprite3nodeSpace.x + *offsetsize;
			if(numberOfRunningActions()==0){
				sprite3action = Follow::createWithOffset(sprite3, -*offsetsize, 300, Rect(0,0, 5200, 800));
				followTheSprite = this->runAction(sprite3action);	
			}
		}
		
		if( (sprite3nodeSpace.x > *lastFollowpos - *offsetsize ) && (sprite3nodeSpace.x < *lastFollowpos + *offsetsize) ){
			this->stopAction(followTheSprite);
		}

this is working in schedule at 0.01f , I used createWithOffset based on low and high limit, but maybe some one know better approach

Why not use getPosition().x , getPosition().y?

I used getPosition and i can check sprite on screen position as above code but dont know this is best approach because createWithOffset made for this issue as I read in this post : Follow action cocos2d-x v3.2. Why always centered followed?

I check all the time for on and off screen.

I think createWithOffset should accept another rect parameter for no-follow zone

auto pos = sprite->getParent()->convertToWorldSpace(sprite->getPosition());

@hoisung thank you

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