How to crop some part of sprite?

谁说我不能喝 提交于 2019-12-12 13:28:02

问题


I am working on a game like tap tap ants(https://itunes.apple.com/us/app/tap-tap-ants/id348839552?mt=8) in iPhone.

I want to crop some portion of sprite. when an ant touches cake sprite, some portion of cake disappear. I researched a lot but could not find any solution.

Plz help


回答1:


You can use spritesheets in the form of CCSpriteBatchNode to set a display frame on a sprite. As done so below. This allows you to select a boxed region of the spritesheet to be displayed.

CCSpriteBatchNode *caveSheet = [CCSpriteBatchNode batchNodeWithFile:@"cavey_ss3.png"];
[self addChild:caveSheet];
CCSprite *player = [CCSprite spriteWithFile:@"somethingUnimportant.png"];

CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:caveSheet.texture rect:CGRectMake(0,0,300,300)];   
//(x-start, y-start, width, height)


[player setDisplayFrame:frame];

Or if you want something like a notch in the corner of the image gone, then you could set up two sprites working off the same CCSpriteBatchNode, with different frames, something like in the picture below.

In this example the frames would be something like this

sprite1 has CGRectMake(0,10,10,40)

sprite 2 has CGRectMake(10,0,20,50)

but ofcourse you would have to position the sprites accordingly aswell.




回答2:


In the very latest version of cocos2d-iphone, they added a class called CCClippingNode. You can use it to clip (show only part of) your node and its contents.

http://www.cocos2d-iphone.org/api-ref/2.1.0/interface_c_c_clipping_node.html



来源:https://stackoverflow.com/questions/15697592/how-to-crop-some-part-of-sprite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!