在Cocos2d中用Sprite绘制两点之间的线条精灵

我一直试图在Xcode上用鼠标事件绘制精灵之间的精灵线。

我一直按照在这个链接论坛上给出的步骤: cocos2d论坛

但是当我运行代码时,我得到了模拟器的所有路线。 像这样。

snapshot1

该行应该停止的第二个鼠标精灵生成的代码,但它不,并继续前进。

我的场景是这样的。

我的课

#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]; 

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

你的问题:

这没有考虑到你正在加载的图像的初始尺寸( line.png )。 如果这不是1×1维度的PNG,那么setScale会使得产生的精灵太大 – 你遇到的超限。

解决scheme

使line.png成为1 x 1像素的图像。 你的代码将完美的工作,虽然你会有一个非常美观的非常细线。

或者,为获得最佳效果,请考虑line.png的宽度来计算精灵的比例 。 这样,精灵可以更详细,不会超出。

setScaleX行更改为:

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

使用Cocos2D v3.x这个工程:

-(void)update:(CCTime)delta{}你这样做:

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

self.drawnode和self.colorDraw属性被初始化,可能在-(void)onEnter{} :

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

我认为你可以在这里使用核心graphics:

 - (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]; } 

我认为这会帮助你。