<legend id='kiKY2'><style id='kiKY2'><dir id='kiKY2'><q id='kiKY2'></q></dir></style></legend>

    <tfoot id='kiKY2'></tfoot>

  1. <small id='kiKY2'></small><noframes id='kiKY2'>

    • <bdo id='kiKY2'></bdo><ul id='kiKY2'></ul>

    <i id='kiKY2'><tr id='kiKY2'><dt id='kiKY2'><q id='kiKY2'><span id='kiKY2'><b id='kiKY2'><form id='kiKY2'><ins id='kiKY2'></ins><ul id='kiKY2'></ul><sub id='kiKY2'></sub></form><legend id='kiKY2'></legend><bdo id='kiKY2'><pre id='kiKY2'><center id='kiKY2'></center></pre></bdo></b><th id='kiKY2'></th></span></q></dt></tr></i><div id='kiKY2'><tfoot id='kiKY2'></tfoot><dl id='kiKY2'><fieldset id='kiKY2'></fieldset></dl></div>

      Cocos2d中Sprites在两点之间画一条线Sprite

      时间:2024-08-12
      <legend id='Vhgwp'><style id='Vhgwp'><dir id='Vhgwp'><q id='Vhgwp'></q></dir></style></legend><tfoot id='Vhgwp'></tfoot>
          <tbody id='Vhgwp'></tbody>

        <i id='Vhgwp'><tr id='Vhgwp'><dt id='Vhgwp'><q id='Vhgwp'><span id='Vhgwp'><b id='Vhgwp'><form id='Vhgwp'><ins id='Vhgwp'></ins><ul id='Vhgwp'></ul><sub id='Vhgwp'></sub></form><legend id='Vhgwp'></legend><bdo id='Vhgwp'><pre id='Vhgwp'><center id='Vhgwp'></center></pre></bdo></b><th id='Vhgwp'></th></span></q></dt></tr></i><div id='Vhgwp'><tfoot id='Vhgwp'></tfoot><dl id='Vhgwp'><fieldset id='Vhgwp'></fieldset></dl></div>

        <small id='Vhgwp'></small><noframes id='Vhgwp'>

        • <bdo id='Vhgwp'></bdo><ul id='Vhgwp'></ul>

                本文介绍了Cocos2d中Sprites在两点之间画一条线Sprite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                I've been trying to draw a sprite line between 2 points made by sprites with mouse events on Xcode.

                I have been following the steps given on a forum in this link: cocos2d forums

                But when i run the code, i get the line going all the way of the simulator. just like this.

                snapshot1

                The line should stop by the second mouse sprite generated code, but it doesn't and keeps going all the way.

                My Scene is something like this.

                My .h class

                #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
                

                My .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
                

                Does anyone knows how to work this out? i have been trying lots of things but nothing has worked for me, or maybe point out my mistake. i would really appreciate this.

                解决方案

                I've not run the code but it looks fairly straightforward. The cause of the problem lies in this section:

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

                This code calculates the distance between the two touch points in points (pixels), creates a new sprite (that will become the line) and sets the anchor point to the right hand side, centred vertically. It positions this at the point of the last touch and then sets the scale of the sprite's width based on the distance calculated earlier. This scaling factor will ensure the sprite is 'long' enough to reach between the two points.

                Your issue:

                This isn't taking into account the initial dimensions of the image you are loading (line.png). If this isn't a 1x1 dimension png then the setScale is going to make the resulting sprite too large - the overrun you are experiencing.

                The Solution

                Make line.png a 1 x 1 pixel image. Your code will work perfectly, though you will have a very thin line that is not aesthetically pleasing.

                Or, for best results, calculate the scale for the sprite by taking into account the width of line.png. This way the sprite can be more detailed and won't overrun.

                Change thesetScaleX line to this:

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

                这篇关于Cocos2d中Sprites在两点之间画一条线Sprite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:填充抗锯齿聚 cocos2d 下一篇:Cocos2D 中的评分系统

                相关文章

              1. <tfoot id='5h7xF'></tfoot>

                <small id='5h7xF'></small><noframes id='5h7xF'>

                  • <bdo id='5h7xF'></bdo><ul id='5h7xF'></ul>

                  <legend id='5h7xF'><style id='5h7xF'><dir id='5h7xF'><q id='5h7xF'></q></dir></style></legend>
                    <i id='5h7xF'><tr id='5h7xF'><dt id='5h7xF'><q id='5h7xF'><span id='5h7xF'><b id='5h7xF'><form id='5h7xF'><ins id='5h7xF'></ins><ul id='5h7xF'></ul><sub id='5h7xF'></sub></form><legend id='5h7xF'></legend><bdo id='5h7xF'><pre id='5h7xF'><center id='5h7xF'></center></pre></bdo></b><th id='5h7xF'></th></span></q></dt></tr></i><div id='5h7xF'><tfoot id='5h7xF'></tfoot><dl id='5h7xF'><fieldset id='5h7xF'></fieldset></dl></div>