简体   繁体   中英

determine if a sprite is on screen or not in cocos2d

i want to determine if the sprite is in the screen or not in cocos2d.

am using the code some thing like these.

CGSize winSize = [CCDirector sharedDirector].winSize;

    if (_SmallBlueAlien1.position.x> 0 || _SmallBlueAlien1.position.x > winSize.width || _SmallBlueAlien1.position.y> 0 || _SmallBlueAlien1.position.y > winSize.height)
    {
//Sprite is not in the screen)
}

but not working properly. were am mistaking. correct me

Unless you changed the anchor point of the sprite this is only testing if half of the sprite is on the screen. To fix this you want to check if

_SmallBlueAlien1.position.x > [_SmallBlueAlien1 contentSize].texture.width / 2;

You can follow this process for all the other interactions.

//Edit

As phix23 noted this does not account for rotation or scale but should work if you are doing neither of those.

regardless of the semantics of 'position' in coco, your '>' should be '<' for both x and y, assuming your interpretation of the .position property holds. It is likely however that the 'sprite'.position is in reference to an enclosing object, thus even when your test is corrected, it may still not give you what you want to know ('visible on screen').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM