简体   繁体   English

确定cocos2d中的精灵是否在屏幕上

[英]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. 我想确定精灵是否在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; _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. 正如phix23所指出的那样,这并不考虑轮换或比例,但是如果你不做这些都不行。

regardless of the semantics of 'position' in coco, your '>' should be '<' for both x and y, assuming your interpretation of the .position property holds. 无论coco中'position'的语义是什么,假设你对.position属性的解释成立,你的'>'对于x和y都应该是'<'。 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'). 然而,'sprite'.position可能是指一个封闭的对象,因此即使你的测试得到纠正,它仍然可能无法提供你想知道的东西('在屏幕上可见')。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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