2012-01-05 62 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];