我试图使用以下代码让我的精灵留在屏幕边框:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self selectSpriteForTouch:touchLocation];
return TRUE; }
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
if (selSprite) {
CGPoint oldLocation = [touch previousLocationInView:touch.view];
oldLocation = [[CCDirector sharedDirector] convertToGL:oldLocation];
oldLocation = [self convertToNodeSpace:oldLocation];
CGPoint translation = ccpSub(touchLocation, oldLocation);
CGPoint newPos = ccpAdd(selSprite.position, translation);
selSprite.position = newPos; } }
我知道我需要添加一些代码来指定边框。如下所示:
int width=point.x;
int height=point.y;
if (winSize.width-sprite.contentSize.width/2<width) {
width=winSize.width-sprite.contentSize.width/2;}
if (winSize.height-sprite.contentSize.height/2<height) {
height=winSize.height-sprite.contentSize.height/2;}
if (sprite.contentSize.height/2>height) {
height=sprite.contentSize.height/2;}
if (sprite.contentSize.width/2>width) {
width=sprite.contentSize.width/2;}
但我是objective C
的新手,我还不完全明白如何处理它。
答案 0 :(得分:0)
if (selSprite) {}
这部分代码
int width=touchLocation.x;
int height=touchLocation.y;
if (selSprite.contentSize.width!=width) {
width=selSprite.contentSize.width/2;}
if (selSprite.contentSize.height/2<height) {
height=selSprite.contentSize.height/2;}
[selSprite setPosition:ccp(width,height)];