cocos2d 抛物线的实现

本文介绍了如何在Cocos2d中实现抛物线的动画效果,起点为240,0坐标。通过代码示例详细解析了抛物线运动的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

启始点为 240,0  横屏的方式



下面为实现在代码

//
//  HelloWorldLayer.h
//  playBackgroundMusic
//
//  Created by Mica001 on 11-9-1.
//  Copyright __MyCompanyName__ 2011年. All rights reserved.
//


// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
//@class PASoundSource ;
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
{
   // PASoundSource *bgTrack;
    CCSprite *grossini;
    CCSprite *tamara;
     float a,b,c;
    float stepx;
    float scale1;
    
}

// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
//-(void) menuCallbackEnable:(id) sender;
-(void)update:(ccTime)dt;
@end

//
//  HelloWorldLayer.m
//  playBackgroundMusic
//
//  Created by Mica001 on 11-9-1.
//  Copyright __MyCompanyName__ 2011年. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"
//#import "SimpleAudioEngine.h"
// HelloWorldLayer implementation
@implementation HelloWorldLayer

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	HelloWorldLayer *layer = [HelloWorldLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
        
        CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
		[cache addSpriteFramesWithFile:@"grossini_blue.plist" textureFile:@"DJ_st_61_52.png"];
        grossini=[CCSprite spriteWithSpriteFrameName:@"grossini_blue_05.png"];   
        tamara=[[CCSprite alloc] initWithFile:@"dm_960_640_01.png"];
		// ask director the the window size
		CGSize size = [[CCDirector sharedDirector] winSize];
		// position the label on the center of the screen
	    grossini.position =  ccp(10 ,100 );
        [self addChild: grossini z:2];        
        
        
        NSMutableArray *animFrames = [NSMutableArray array];
		for(int i = 1; i < 6; i++) {
			
			CCSpriteFrame *frame = [cache spriteFrameByName:[NSString stringWithFormat:@"grossini_blue_%02d.png",i]];
            NSLog(@"%@",[NSString stringWithFormat:@"grossini_blue_%02d.png",i]);
			[animFrames addObject:frame];
		}
        
		CCAnimation *animation = [CCAnimation animationWithFrames:animFrames];
		// 14 frames * 1sec = 14 seconds
		[grossini runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithDuration:0.3f animation:animation restoreOriginalFrame:NO] ]];
        
		// to test issue #732, uncomment the following line
		grossini.flipX = NO;
		grossini.flipY = NO;
        
       // grossini.scale=0.3f;
        
        
        
        grossini.position =ccp(size.width/2,0);
        
        
        
        
        scale1=1.0f;
        
        tamara.position=ccp(size.width/2,size.height );
        tamara.scale=0.5;
       
        [self setIsTouchEnabled:true];
        [self addChild:tamara z:1];
	}
	return self;
}

-(void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
	return YES;
}
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {  
    CGPoint touchLocation = [touch locationInView: [touch view]];		
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];    
    touchLocation = [self convertToNodeSpace:touchLocation];
    grossini.position=CGPointMake(240,0);
    CGPoint startPoint = grossini.position;
    CGPoint endPoint = touchLocation;
    float x1 =  startPoint.x; //起始点
    float y1 = startPoint.y;
    float x3 =endPoint.x;//终点
    float y3 = endPoint.y;
    //发射路径宽度

    //算出中间会经过的一点
    
    scale1= 1/ y3  ;
    grossini.scale =1.0f;
    
    float x2 ,y2;
    
    if (endPoint.x>=240) {  //  右边
        x2 = (x3-x1)/2+x1;
         y2 =y3/2+170;
        stepx=0.5f;
    }else   //左边
    {
        x2 = (x1-x3)/2+x3;
        y2 =y3/2+170;
        stepx=-0.5f;
    }
    //根据抛物线方程ax^2+bx+c=y,得方程组 //ax1^2+bx1+c=y1 //ax2^2+bx2+c=y2 //ax3^2+bx3+c=y3 //解方程组得抛物线的a,b,c
    
    b = ((y1-y3)*(x1*x1-x2*x2)-(y1-y2)*(x1*x1-x3*x3))/((x1-x3)*(x1*x1-x2*x2)-(x1-x2)*(x1*x1-x3*x3)); 
    a = ((y1-y2)-b*(x1-x2))/(x1*x1-x2*x2);
    c = y1-a*x1*x1-b*x1;    
    
    [self schedule:@selector(update:) ];

}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}

- (void)update:(ccTime)dt {
    
    CGPoint position = grossini.position;
    float x = position.x +stepx;
    float y = a* x * x + b*x + c;
    grossini.position = ccp(x, y);
    grossini.scale -=0.002;

    NSLog(@"x=%f  y=%f scale1=%f",grossini.position.x,grossini.position.y, scale1);
}

// on "dealloc" you need to release all your retained objects
- (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"
    [grossini release];
	[super dealloc];
}
@end


这里只是抛物线的实现,,

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值