I would like to have a CCSprite move to a specific location, while, at the same time, drawing a line along its movement path. Any ideas? Thanks a lot.
CCMoveTo with line drawing
(10 posts) (3 voices)-
Posted 1 year ago #
-
I am doing something very similar, see this post here, the last one in the thread.
http://www.cocos2d-iphone.org/forum/topic/4497#post-28965
I haven't worked out all the kinks yet, but that's the idea.
Posted 1 year ago # -
Thanks slycrel. I am still new to Cocos2d, how do I create a pen sprite?
Also, I was expecting to see actions like CCActionDrawTo in Cocos2d, with parameters like "actionWidthDuration" and "position". Any idea how to go about developing something like that? Do you guys think this is something worth developing and adding to the main classes of Cocos2d?
Posted 1 year ago # -
the pen sprite I am currently using is a 8x8 pixel circle that is white. You can (and I have, just for fun) replace this with any sprite.
Start with getting a feel for the actions, look at the cocos2d example project for RenderTexture and for the actions. That should give you a headstart.
Rather than adding this specifically to cocos2d I think I would lean towards adding a selector that could be called from any action at the same time that update is run. more like making update a delegate function rather than having to override the action itself.
Posted 1 year ago # -
Thanks again slycrel. I am still unable to achieve a pen sprite, would you please post some code?
Posted 1 year ago # -
After some trials, I was able to create what I want via creating a CCPenSprite class. The code is still a first draft:
@interface CCPenSprite : CCSprite { BOOL penDown; CCRenderTexture *renderTexture; } // Set penDown to TRUE for the Sprite to draw during movement @property (nonatomic, readwrite) BOOL penDown; // The RenderTexture that the pen will draw to, must be set for the pen to draw @property (nonatomic, readwrite, assign) CCRenderTexture *renderTexture; @end @implementation CCPenSprite @synthesize penDown; @synthesize renderTexture; -(id) init { if ( (self = [super init]) ) { self.penDown = FALSE; self.renderTexture = nil; } return self; } -(void) dealloc { [super dealloc]; } -(void)setPosition:(CGPoint)pos { if (self.penDown && self.renderTexture!=nil) { renderTexture.begin; ccDrawLine(position_, pos); renderTexture.end; } [super setPosition:pos]; } @endMy next steps will be adding customization to what the CCPenSprite actually draws while it moves.
Posted 1 year ago # -
Hmm, looks nice. Once its finished I hope they add it to cocos2d
Posted 1 year ago # -
Hi tarekskr,
essentially that is what I am doing, though I am drawing to a renderTexture using a sprite from an action. See some of my code here:
http://www.cocos2d-iphone.org/forum/topic/4884#post-29367The method I am using is not foolproof, if my action time is to small then my visits will be too infrequent, and I get dots rather than lines. I plan to do some math similar to the current action math to take care of that problem and add a for loop to the update function. This will make the update function draw the sprite as many times as needed to fill the space between updates regardless of if the pen sprite is too small for the space moved over the update time.
I am still struggling with actions and items having unexpected positions. I plan to work more this evening on that, starting with porting my code from the iPad simulator to the iPhone simulator and see if I have the same kinds of problems there. Thanks for sharing your code and your interest. Glad it's working out for you. =)
Posted 1 year ago # -
Hi slycrel:
Why don't you use my CCPenSprite class? This should solve your dots problem. If your action time is too small, then worst you could get with my method is irregular (flat) curve segments, but you should never get dots. Hope it's of help to you.
Posted 1 year ago # -
tarekskr,
I appreciate the thought. I almost did it that way initially, but I've changed my paradigm. The reason I am wanting to use the sprite based approach rather than ccDrawLine() is that I want to be able to redefine my "pen" in various different ways. So if I want to have one pen that is 4x4 pixel square and another that is 30x30 pixel circle I can do that with my sprite-based approach without having to change the openGL pen states. (I don't know enough about openGL, just that I could probably do it, but not how to go about it drawing directly) So the way I am doing it, I can plug in any "pen" that I want and have it use that to draw with, which is appealing for what I am working on.
Posted 1 year ago #
Reply
You must log in to post.