 |
 |
|
 |
02-07-2010, 04:06 PM
|
#1 (permalink)
|
|
Registered Member
Join Date: Feb 2010
Posts: 8
|
Animate a sprite
Hi, im creating an app similar to doodle jump where my main sprite has a negative acceleration and a positive velocity whenever it touches the platforms, and i want to animate the sprite as it hits the platform, but i have no idea how to go about doing that. Can anyone give me some suggestions?
|
|
|
02-07-2010, 04:25 PM
|
#2 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 77
|
Assuming your sprite is a UIImageView:
Code:
myImageview.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"frame_1.png"],[UIImage imageNamed:@"frame_2.png"],nil];
myImageview.animationDuration=0.5;myImageview.animationRepeatCount=0;[myImageview startAnimating];
where animationDuration=0.5 is the total duration of the animation in seconds.
|
|
|
02-07-2010, 04:42 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Feb 2010
Posts: 8
|
Quote:
Originally Posted by ChrisMayer
Assuming your sprite is a UIImageView:
Code:
myImageview.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"frame_1.png"],[UIImage imageNamed:@"frame_2.png"],nil];
myImageview.animationDuration=0.5;myImageview.animationRepeatCount=0;[myImageview startAnimating];
where animationDuration=0.5 is the total duration of the animation in seconds.
|
i tried that, but i dont know where to declare the myImageview. Also, what would happen if my sprite wasnt a UIImageView? something like:
AtlasSpriteManager *spriteManager = (AtlasSpriteManager*)[self getChildByTag:kSpriteManager];
AtlasSprite *one = (AtlasSprite*)[spriteManager getChildByTag:kOne];
|
|
|
02-07-2010, 04:47 PM
|
#4 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 77
|
Ah, you're using cocos2d. Unfortunatly I've no experience with that, I usually do everything the manual way.
|
|
|
02-07-2010, 10:19 PM
|
#5 (permalink)
|
|
Registered Member
Join Date: Feb 2010
Posts: 8
|
Also, in cocos2d i have a case statement to display the different platforms, but i want one of these cases to have a different value for the velocity of the sprite, so how would i go about doing that?
|
|
|
02-08-2010, 12:48 AM
|
#6 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 125
|
you can use this to animate a sprite
id animation = [MoveBy actionWithDuration:2.0 position:ccp(-100, 0)]; // Make the animation MoveBy tells the sprite to move "so many pixels" from it's current position
[sprite runAction: animation]; // Tell the stickman to do the animation
though i am looking for animate a spirte sheet (having two images 1 at 0,100 and toher at 10,100)
|
|
|
02-08-2010, 04:42 PM
|
#7 (permalink)
|
|
Registered Member
Join Date: Feb 2010
Posts: 8
|
Those are all good answers, but thats not exactly what im looking for. Let me try it another way:
I initialized an AtlasSprite in my (id) init, which is my main sprite
Then in (void) jump, i want to take that same sprite, but i want to animate it with an array of images. The images i want to animate it with are called 2.png, 3.png, 4.png, and 5.png. Here is some code:
- (id)init {
AtlasSprite *one = [AtlasSprite spriteWithRect:CGRectMake(608,-7,60,70) spriteManager:spriteManager];
[spriteManager addChild  ne z:4 tag:kOne];
}
- (void)jump {
one_vel.y = 550.0f;
//This is where i want to animate "one" with an array of images called //2.png, 3.png, 4.png, and 5.png
//Also, how would i declare the atlas sprite used above in (void) jump?
}
|
|
|
02-08-2010, 08:08 PM
|
#8 (permalink)
|
|
Registered Member
Join Date: Feb 2010
Posts: 8
|
i ran into another problem also. In the code below i have initialized 3 platforms, and this code works, but i want to add a special feature to the case 2 platform. I want to make an if statement saying that if my sprite touches it, the sprite's velocity will be different, but i tried all the ways i can think of and have no idea how to continue.
- (void)initPlatform {
CGRect rect;
switch(random()%3) {
case 0: rect = CGRectMake(633,83,64,13); break;
case 1: rect = CGRectMake(633,139,65,14); break;
case 2: rect = CGRectMake(725,80,78,16); break;
}
|
|
|
02-08-2010, 11:38 PM
|
#9 (permalink)
|
|
Registered Member
Join Date: Aug 2009
Posts: 125
|
try this
id anim = [[Animation alloc] initWithName:@"fight" delay:0.1];
[anim AddFrameWithName:@"goblin_swing_01"];
[anim AddFrameWithName:@"goblin_swing_01"];
[anim AddFrameWithName:@"goblin_swing_02"];
[anim AddEvent:@"something"];
[anim AddFrameWithName:@"goblin_swing_03"];
[anim AddFrameWithName:@"goblin_swing_02"];
[someSprite runAction:[Animate action:anim withDelegate:self]];
|
|
|
02-09-2010, 03:59 PM
|
#10 (permalink)
|
|
Registered Member
Join Date: Feb 2010
Posts: 8
|
thank you very much! That code you provided works perfectly! but i still need help with my second question...
|
|
|
02-10-2010, 09:59 PM
|
#11 (permalink)
|
|
Registered Member
Join Date: Feb 2010
Posts: 8
|
Anybody know?
|
|
|
02-11-2010, 11:13 PM
|
#12 (permalink)
|
|
Registered Member
Join Date: Feb 2010
Posts: 8
|
bump
please help me, i have no clue what to do still
|
|
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
» Advertisements |
» Online Users: 377 |
| 33 members and 344 guests |
| acmecorps, bandley, bensj, bobvanratingen, brianr, chouchou, CHV, cmazzi, Cooper_1988, DaveM, drakan, eieiosoftware, firearasi, gandohr, gbh, goodcode, henmue, iGeorG, Kilby, lbergelt, nikozster, omerk, pengcognito, pikabli, quky, Raazo, Rainer, shark4ever, SkipLo, supernico, Trindaz, wassupdoc, yeohchan |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,292
Threads: 39,080
Posts: 171,361
Top Poster: smasher (2,575)
|
| Welcome to our newest member, omerk |
|