2012-01-05 85 views
1

我一直試圖在Xcode上用鼠標事件創建兩個精靈之間的精靈直線。在Cocos2d中創建兩個點之間的直線精靈

我一直在下面在這個鏈接的論壇的步驟: cocos2d forums

但是當我運行的代碼,我得到的線去模擬器的所有道路。像這樣。

snapshot1

該行應停止第二隻老鼠精靈生成的代碼,但它不和一直將所有的方式。

我的場景是這樣的。

我的.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 

有誰知道如何工作了這一點?我一直在嘗試很多東西,但沒有爲我工作,或者可能指出我的錯誤。我真的很感激這一點。

回答

3

我沒有運行代碼,但它看起來相當簡單。問題的原因在於本節:

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.png)。如果這不是1x1尺寸的PNG,那麼setScale會使得產生的精靈太大 - 你遇到的超載。

line.png一個1×1個像素的圖像。你的代碼可以很好地工作,儘管你會有一條非常美麗的線條,而且不美觀。

或者,爲獲得最佳效果,請考慮line.png的寬度來計算精靈的比例。這樣,精靈可以更詳細,不會超出。

更改setScaleX行這樣的:

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

你說得對,我不敢相信那是這樣的,我感謝你的幫助davbryn。根據邊界框放置setScaleX完美地工作。非常感謝。 – DanMeza 2012-01-06 18:09:00

-1

我認爲你可以在這裏使用核心顯卡:

- (void)drawRect:(CGRect)rect { 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextSetLineWidth(context,4); 
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 


    CGContextMoveToPoint(context,startPoint.x , startPoint.y); 
    CGContextAddLineToPoint(context, endPoint.x, endPoint.y); 
    CGContextStrokePath(context); 

} 
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    UITouch* touchPoint = [touches anyObject]; 
    startPoint = [touchPoint locationInView:self]; 
    endPoint = [touchPoint locationInView:self]; 

    [self setNeedsDisplay]; 
} 

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch* touch = [touches anyObject]; 
    endPoint=[touch locationInView:self]; 
    [self setNeedsDisplay]; 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch* touch = [touches anyObject]; 
    endPoint = [touch locationInView:self]; 
    [self setNeedsDisplay]; 
} 

我認爲這會幫助你。

+0

他正在使用Cocos2D爲這個項目使用OpenGL進行渲染。我認爲在這種情況下嘗試將它與Core Graphics結合起來並不理想。 – davbryn 2012-01-06 15:51:09

+0

對不起,我用cocos2d核心顯卡,發現方式很簡單,核心顯卡也是基於openGL,那麼使用openGL或核心顯卡有什麼區別。請解釋.. – Haroon 2012-01-06 19:47:30

+1

核心圖形是基於Quartz而不是OpenGL的。他們並沒有真正合作 – davbryn 2012-01-07 14:26:20

0

使用cocos2d的3.x版這個工程:

-(void)update:(CCTime)delta{}你這樣做:

[self.drawnode drawSegmentFrom:ccp(50,100) to:ccp(75, 25) radius:3 color:self.colorDraw]; 

的self.drawnode和自我。 colorDraw屬性是這樣初始化的,也許在裏面-(void)onEnter{} :

self.drawnode = [CCDrawNode node]; 
self.colorDraw = [CCColor colorWithCcColor3b:ccRED]; 
[self addChild:self.drawnode];