ios - 在 Cocos2d 中的 Sprites 制作的两点之间绘制一条线 Sprite

标签 ios xcode cocos2d-iphone sprite

我一直在尝试在 Xcode 上使用鼠标事件的 Sprite 制作的 2 个点之间绘制一条 Sprite 线。

我一直在遵循此链接中论坛上给出的步骤: cocos2d forums

但是当我运行代码时,我得到的线路一直延伸到模拟器。就像这样。

snapshot1

该行应该在第二个鼠标 Sprite 生成的代码处停止,但它并没有停止,而是一直继续下去。

我的场景是这样的。

我的 .h 类

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Constants.h"
#import "SceneManager.h"


@interface EscenaInfo : CCLayer{  
    CGPoint lastTouchPoint;        
    CCSprite * background;
}

@property (nonatomic, assign) BOOL iPad;

@end

我的.mm

#import "EscenaInfo.h"  

@implementation EscenaInfo  
@synthesize iPad;


- (void)onBack: (id) sender {
    /* 
     This is where you choose where clicking 'back' sends you.
     */
    [SceneManager goMenuPrincipal];
}

- (void)addBackButton {

    if (self.iPad) {
        // Create a menu image button for iPad
        CCMenuItemImage *goBack = [CCMenuItemImage itemFromNormalImage:@"Arrow-Normal-iPad.png" 
                                                         selectedImage:@"Arrow-Selected-iPad.png"
                                                                target:self 
                                                              selector:@selector(onBack:)];
        // Add menu image to menu
        CCMenu *back = [CCMenu menuWithItems: goBack, nil];

        // position menu in the bottom left of the screen (0,0 starts bottom left)
        back.position = ccp(64, 64);

        // Add menu to this scene
        [self addChild: back];
    }
    else {
        // Create a menu image button for iPhone / iPod Touch
        CCMenuItemImage *goBack = [CCMenuItemImage itemFromNormalImage:@"Arrow-Normal-iPhone.png" 
                                                         selectedImage:@"Arrow-Selected-iPhone.png"
                                                                target:self 
                                                              selector:@selector(onBack:)];
        // Add menu image to menu
        CCMenu *back = [CCMenu menuWithItems: goBack, nil];

        // position menu in the bottom left of the screen (0,0 starts bottom left)
        back.position = ccp(32, 32);

        // Add menu to this scene
        [self addChild: back];        
    }
}

- (id)init {

    if( (self=[super init])) {

        // Determine Screen Size
        CGSize screenSize = [CCDirector sharedDirector].winSize;  

        //Boton en la Interfaz del iPad
        self.iPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;

        //  Put a 'back' button in the scene
        [self addBackButton]; 

        ///
        self.isTouchEnabled = YES;
        lastTouchPoint = ccp(-1.0f,-1.0f);                       
        ///

        [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGB565];
        background = [CCSprite spriteWithFile:@"background.png"];
        background.anchorPoint = ccp(0,0);
        [self addChild:background z:-1];
        [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_Default];

    }
    return self;
}

- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)

    // don't forget to call "super dealloc"
    [super dealloc];
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if( touch ) {
        CGPoint location = [touch locationInView: [touch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];
        CCLOG(@"location(%f,%f)", location.x, location.y);

        if( CGPointEqualToPoint(lastTouchPoint, ccp(-1.0f,-1.0f) ) )
        {
            lastTouchPoint = ccp(location.x, location.y);
            CCSprite *circle = [CCSprite spriteWithFile:@"circle.png"];
            [circle setPosition:lastTouchPoint];
            [self addChild:circle];
            CCLOG(@"initial touchpoint set. to (%f,%f)", lastTouchPoint.x, lastTouchPoint.y);
        }
        else {
            CCLOG(@"lastTouchPoint is now(%f,%f), location is (%f,%f)", lastTouchPoint.x, lastTouchPoint.y, location.x, location.y);
            CGPoint diff = ccpSub(location, lastTouchPoint);
            float rads = atan2f( diff.y, diff.x);
            float degs = -CC_RADIANS_TO_DEGREES(rads);
            float dist = ccpDistance(lastTouchPoint, location);
            CCSprite *line = [CCSprite spriteWithFile:@"line.png"];
            [line setAnchorPoint:ccp(0.0f, 0.5f)];
            [line setPosition:lastTouchPoint];
            [line setScaleX:dist];
            [line setRotation: degs];
            [self addChild:line];

            CCSprite *circle = [CCSprite spriteWithFile:@"circle.png"];
            [circle setPosition:location];
            [self addChild:circle];

            //          lastTouchPoint = ccp(location.x, location.y);
            lastTouchPoint = ccp(-1.0f,-1.0f);
        }

    }
}
@end

有人知道怎么解决吗?我一直在尝试很多事情,但没有对我有用,或者可能会指出我的错误。我真的很感激。

最佳答案

我没有运行代码,但它看起来相当简单。问题的原因出在这一段:

float dist = ccpDistance(lastTouchPoint, location);
CCSprite *line = [CCSprite spriteWithFile:@"line.png"];
[line setAnchorPoint:ccp(0.0f, 0.5f)];
[line setPosition:lastTouchPoint];
[line setScaleX:dist];

此代码计算两个触摸点之间的距离(以点(像素)为单位),创建一个新 Sprite (将成为线)并将 anchor 设置在右侧,垂直居中。它将其定位在最后一次触摸点,然后根据之前计算的距离设置 Sprite 宽度的比例。这个比例因子将确保 Sprite “长”到足以到达两点之间。

您的问题:

这没有考虑您正在加载的图像的初始尺寸 (line.png)。如果这不是 1x1 尺寸的 png,那么 setScale 将使生成的 sprite 太大 - 您遇到的超限。

解决方案

使 line.png 成为 1 x 1 像素的图像。您的代码将完美运行,尽管您的代码非常细且不美观。

或者,为了获得最佳效果,通过考虑 line.png 的宽度来计算 Sprite 的比例。这样 Sprite 可以更详细并且不会溢出。

setScaleX 行更改为:

[line setScaleX:dist / line.boundingBox.size.width];

关于ios - 在 Cocos2d 中的 Sprites 制作的两点之间绘制一条线 Sprite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8749853/

相关文章:

javascript - Cocos2d html5教程返回空白页

ios - 收到消息 "WARNING: under normal conditions, _fillInQueueWithExtraSpace:.."并且 MPMoviePlayer 旋转在 iPad IOS 5.1 中不起作用

ios - 如何在 UIPickerView 中只显示一行?

ios - 在 uiimage 中以正确的方式快速显示小或大的高度、宽度图像

xcode - Swift 中的直播

ios - 如何在 Cocos2D 中使 Sprite 在更新循环内跳转

iphone - 通过试飞了解 iOS 崩溃报告

ios - 通过推送通知 Objective C iOS 从 Kill 状态打开时应用程序崩溃

xcode - swift 错误 : EXE_BAD_INSTRUCTION (code=EXC_I386_INVOP, 子代码 = 0x0)

ios - 替换场景继续增加内存使用和内存泄漏如何处理?或者我应该如何在我的项目中启用 ARC