开发者

determine if a sprite is on screen or not in cocos2d

开发者 https://www.devze.com 2023-04-01 09:40 出处:网络
i want to determine if the sprite is in the scr开发者_JAVA技巧een or not in cocos2d. am using the code some thing like these.

i want to determine if the sprite is in the scr开发者_JAVA技巧een 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').

0

精彩评论

暂无评论...
验证码 换一张
取 消