Cocos2d拍摄方法

发布于 2024-12-28 07:37:01 字数 4969 浏览 0 评论 0原文

好的,我是编码和 cocos2d 的新手 我有这个射击代码,可以发射射弹,当我尝试在屏幕左侧发射时,射弹会从球的位置向右下方发射?

这是我的 GamePlay.m

#import "GamePlay.h"

CCSprite *player;
CCSprite *grass;
CCSprite *gameBg;

@implementation GamePlay

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    GamePlay *layer = [GamePlay node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) 
    {
        self.isTouchEnabled = YES;

        gameBg = [CCSprite spriteWithFile:@"backgroundGame1.png"];
        gameBg.position = ccp(240,160);
        [self addChild:gameBg];

        grass = [CCSprite spriteWithFile:@"grass.jpg"];
        grass.position = ccp(240,25);
        [self addChild:grass];

        player = [CCSprite spriteWithFile:@"ball.png"];
        player.position = ccp(27,95);
        [self addChild:player];

        x = 5;
        y = 5;
    }
    return self;
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *myTouch = [touches anyObject];
    CGPoint point = [myTouch locationInView:[myTouch view]];
    point = [[CCDirector sharedDirector] convertToGL:point];

    if (point.x > 240 && point.y < 150) 
    {
        [self unschedule:@selector(moveLeft)];
        [self schedule:@selector(moveRight) interval:.01];
    }
    if (point.x < 240 && point.y < 150) 
    {
        [self unschedule:@selector(moveRight)];
        [self schedule:@selector(moveLeft) interval:.01];
    }
    NSLog(@"Touch Began");
    // Choose one of the touches to work with

    if (point.y > 150) 
    {
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];

        CGSize winSize = [[CCDirector sharedDirector]winSize];
        CCSprite *projectile = [CCSprite spriteWithFile:@"projectile.png"];
        projectile.position = ccp(player.position.x,player.position.y);

       int offX =   location.x - projectile.position.x; 
       int offY =   location.y - projectile.position.y; 

       [self addChild:projectile];

       int realX = winSize.width + (projectile.contentSize.width/2);
       float ratio = (float) offY / (float) offX;
       int realY = (realX *ratio) + projectile.position.y;
       CGPoint realDest = ccp(realX, realY);

       int offRealX = realX - projectile.position.x;
       int offRealY = realY - projectile.position.y;
       float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
       float velocity = 480/1;
       float realMoveDuration = length/velocity;

       [projectile runAction:[CCMoveTo actionWithDuration:realMoveDuration position:realDest]];

       NSLog(@"Shoot!");
    }    
}

-(void)ccTouchesEnded:(NSSet *) touches withEvent:(UIEvent *)event  
{
    UITouch *myTouch = [touches anyObject];
    CGPoint point = [myTouch locationInView:[myTouch view]];
    point = [[CCDirector sharedDirector] convertToGL:point];

   [self unschedule:@selector(moveLeft)];
   [self unschedule:@selector(moveRight)];

   NSLog(@"Touch Ended");
}

-(void) spriteMoveFinished: (id) sender 
{
}


-(void)moveLeft 
{

    player.position = ccp(player.position.x - x, player.position.y);
    if (player.position.x < 15) 
    {
        player.position = ccp(16,player.position.y);
    } 
}

-(void)moveRight 
{
    player.position = ccp(player.position.x + x, player.position.y);
    if (player.position.x > 465) 
    {
        player.position = ccp(464,player.position.y);
    }
}

@end

这是射击方法(我认为它与 x & y 偏移有关?)

if (point.y > 150) 
    {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    CGSize winSize = [[CCDirector sharedDirector]winSize];
    CCSprite *projectile = [CCSprite spriteWithFile:@"projectile.png"];
    projectile.position = ccp(player.position.x,player.position.y);

   int offX =   location.x - projectile.position.x; 
   int offY =   location.y - projectile.position.y; 

   [self addChild:projectile];

   int realX = winSize.width + (projectile.contentSize.width/2);
   float ratio = (float) offY / (float) offX;
   int realY = (realX *ratio) + projectile.position.y;
   CGPoint realDest = ccp(realX, realY);

   int offRealX = realX - projectile.position.x;
   int offRealY = realY - projectile.position.y;
   float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
   float velocity = 480/1;
   float realMoveDuration = length/velocity;

   [projectile runAction:[CCMoveTo actionWithDuration:realMoveDuration position:realDest]];

   NSLog(@"Shoot!");
}    

ok i am new to coding and cocos2d
i have this shooting code that will fire a projectile and when i try to fire on the left side of the screen it the projectile is fired down and right from the position of the ball?

heres my GamePlay.m

#import "GamePlay.h"

CCSprite *player;
CCSprite *grass;
CCSprite *gameBg;

@implementation GamePlay

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    GamePlay *layer = [GamePlay node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) 
    {
        self.isTouchEnabled = YES;

        gameBg = [CCSprite spriteWithFile:@"backgroundGame1.png"];
        gameBg.position = ccp(240,160);
        [self addChild:gameBg];

        grass = [CCSprite spriteWithFile:@"grass.jpg"];
        grass.position = ccp(240,25);
        [self addChild:grass];

        player = [CCSprite spriteWithFile:@"ball.png"];
        player.position = ccp(27,95);
        [self addChild:player];

        x = 5;
        y = 5;
    }
    return self;
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *myTouch = [touches anyObject];
    CGPoint point = [myTouch locationInView:[myTouch view]];
    point = [[CCDirector sharedDirector] convertToGL:point];

    if (point.x > 240 && point.y < 150) 
    {
        [self unschedule:@selector(moveLeft)];
        [self schedule:@selector(moveRight) interval:.01];
    }
    if (point.x < 240 && point.y < 150) 
    {
        [self unschedule:@selector(moveRight)];
        [self schedule:@selector(moveLeft) interval:.01];
    }
    NSLog(@"Touch Began");
    // Choose one of the touches to work with

    if (point.y > 150) 
    {
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];

        CGSize winSize = [[CCDirector sharedDirector]winSize];
        CCSprite *projectile = [CCSprite spriteWithFile:@"projectile.png"];
        projectile.position = ccp(player.position.x,player.position.y);

       int offX =   location.x - projectile.position.x; 
       int offY =   location.y - projectile.position.y; 

       [self addChild:projectile];

       int realX = winSize.width + (projectile.contentSize.width/2);
       float ratio = (float) offY / (float) offX;
       int realY = (realX *ratio) + projectile.position.y;
       CGPoint realDest = ccp(realX, realY);

       int offRealX = realX - projectile.position.x;
       int offRealY = realY - projectile.position.y;
       float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
       float velocity = 480/1;
       float realMoveDuration = length/velocity;

       [projectile runAction:[CCMoveTo actionWithDuration:realMoveDuration position:realDest]];

       NSLog(@"Shoot!");
    }    
}

-(void)ccTouchesEnded:(NSSet *) touches withEvent:(UIEvent *)event  
{
    UITouch *myTouch = [touches anyObject];
    CGPoint point = [myTouch locationInView:[myTouch view]];
    point = [[CCDirector sharedDirector] convertToGL:point];

   [self unschedule:@selector(moveLeft)];
   [self unschedule:@selector(moveRight)];

   NSLog(@"Touch Ended");
}

-(void) spriteMoveFinished: (id) sender 
{
}


-(void)moveLeft 
{

    player.position = ccp(player.position.x - x, player.position.y);
    if (player.position.x < 15) 
    {
        player.position = ccp(16,player.position.y);
    } 
}

-(void)moveRight 
{
    player.position = ccp(player.position.x + x, player.position.y);
    if (player.position.x > 465) 
    {
        player.position = ccp(464,player.position.y);
    }
}

@end

this is the shooting method (i think it has something to do with the x & y offset?)

if (point.y > 150) 
    {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    CGSize winSize = [[CCDirector sharedDirector]winSize];
    CCSprite *projectile = [CCSprite spriteWithFile:@"projectile.png"];
    projectile.position = ccp(player.position.x,player.position.y);

   int offX =   location.x - projectile.position.x; 
   int offY =   location.y - projectile.position.y; 

   [self addChild:projectile];

   int realX = winSize.width + (projectile.contentSize.width/2);
   float ratio = (float) offY / (float) offX;
   int realY = (realX *ratio) + projectile.position.y;
   CGPoint realDest = ccp(realX, realY);

   int offRealX = realX - projectile.position.x;
   int offRealY = realY - projectile.position.y;
   float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
   float velocity = 480/1;
   float realMoveDuration = length/velocity;

   [projectile runAction:[CCMoveTo actionWithDuration:realMoveDuration position:realDest]];

   NSLog(@"Shoot!");
}    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙厌世 2025-01-04 07:37:01

最好的资源是这里

尝试一下。
干杯

The best resource is here

Just give it a try.
Cheers

铃予 2025-01-04 07:37:01

我在这里找到了答案 Projectiles/Bullets 方向 Cocos2d
你需要这样做,

// After adding the projectile:
[self addChild:projectile];

// Add a scalar float:
float scalarX = 1.0f;

// And make it negative if the touch is left of the character:
if (offX < 0.0f) scalar = -1.0f;

// Then just multiply the realX by this scalar to make it point the correct way
int realX = scalar * (winSize.width + (projectile.contentSize.width/2));

I found the answer here Projectiles/Bullets direction Cocos2d
you needed to do this,

// After adding the projectile:
[self addChild:projectile];

// Add a scalar float:
float scalarX = 1.0f;

// And make it negative if the touch is left of the character:
if (offX < 0.0f) scalar = -1.0f;

// Then just multiply the realX by this scalar to make it point the correct way
int realX = scalar * (winSize.width + (projectile.contentSize.width/2));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文