Cocos2d EXC_BAD_ACCESS

2023-12-19

我是 cocos2d 的新手,突然得到了这个 EXC_BAD_ACCESS, 我做了一个新的获胜菜单,但出现了错误 我认为错误是因为我调用了一个已释放的对象,但我没有释放任何东西? 我的调试控制台没有错误,这很奇怪

这是我的 Level_1.m

//
//  Level_1.m
//  iPadGame
//
//  Created by My Name on 1/25/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "Level_1.h"
#import "HelloWorldLayer.h"

CCSprite *player;
CCSprite *enemy;
CCSprite *enemy2;
CCSprite *enemy3;
CCSprite *star;
CCSprite *star2;
CCSprite *star3;
CCSprite *bg;
CCSprite *toolBar;

CCLabelTTF *youWin;

bool movePlayer;


@implementation Level_1

@synthesize score;

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

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

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

    // return the scene
    return scene;
}

-(void) setUpWinMenu {
    [CCMenuItemFont setFontName:@"Marker Felt"];
    [CCMenuItemFont setFontSize:75];
    CCMenuItem *MainMenu = [CCMenuItemFont itemFromString:@"Main Menu" target:self selector:@selector(gotoMainMenu)];
    CCMenu *WinMenu = [CCMenu menuWithItems:MainMenu, nil];
    [self addChild:WinMenu];
    MainMenu.position = ccp(400,500);

}
// 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])) 
    {
        self.isTouchEnabled = YES;

        scoreNumber = 10;

        bg = [CCSprite spriteWithFile:@"metal_background.jpeg"];
        bg.position = ccp(512,384);
        [self addChild:bg];

        toolBar = [CCSprite spriteWithFile:@"ToolBar.png"];
        toolBar.position = ccp(512,-30);
        [self addChild:toolBar];

        score = [CCLabelAtlas labelWithString:@"0123456789" charMapFile:@"ScoreFinal.png" itemWidth:50 itemHeight:75 startCharMap:'.'];
        [self addChild:score];
        score.position = ccp (-100,15);

        CCLabelTTF *scoreLabel = [CCLabelTTF labelWithString:@"Score:" fontName:@"Marker Felt" fontSize:45];
        scoreLabel.position = ccp(score.position.x + 275,score.position.y + 40);
        scoreLabel.color = ccc3(0, 0, 0);
        [self addChild:scoreLabel];



        star = [CCSprite spriteWithFile:@"Star.png"];
        star.position = ccp(400,600);
        [self addChild:star];

        star2 = [CCSprite spriteWithFile:@"Star.png"];
        star2.position = ccp(600,600);

        star3 = [CCSprite spriteWithFile:@"Star.png"];
        star3.position = ccp(200,600);

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

        enemy = [CCSprite spriteWithFile:@"SpaceShip.png"];
        enemy.position = ccp(150,600);
        [self addChild:enemy];

        enemy2 = [CCSprite spriteWithFile:@"SpaceShip.png"];
        enemy2.position = ccp(250,600);
        [self addChild:enemy2];

        enemy3 = [CCSprite spriteWithFile:@"SpaceShip.png"];
        enemy3.position = ccp(350,600);
        [self addChild:enemy3];

        [self schedule:@selector(enemyMove) interval:0.01];
        [self schedule:@selector(collisionDetection) interval:0.01];
        [self schedule:@selector(getStar) interval:0.01];

        NSString *string = [NSString stringWithFormat:@"Score: %i", (int)scoreNumber];
        [score setString:string];

        x = 15;
        x2 = 15;
        x3 = 15;
        y = 15;

        Bx = 10;
        By = 10;

        movePlayer = FALSE;
        CCRepeatForever *repeat = [CCRepeatForever actionWithAction: [CCRotateBy actionWithDuration:2 angle:360]];
        [star runAction:repeat];

        star.visible = 1;

    }
    return self;
}


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


    CGRect playerRect = CGRectMake(player.position.x - (player.contentSize.width/2),
                                   player.position.y - (player.contentSize.height/2),
                                   player.contentSize.width,
                                   player.contentSize.height);

    CGRect Tlocation = CGRectMake(location.x, location.y, 10, 10);



    NSLog(@"Touch Began");
    if (CGRectIntersectsRect (Tlocation, playerRect)) {
        player.position = location;
        movePlayer = TRUE;
    }



}

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

    if (movePlayer == TRUE) {
        player.position = point;

        if (player.position.y < 110) {
            player.position = ccp(player.position.x, 111);
        }
    }

}

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touch ended");

    movePlayer = FALSE;

}

-(void) enemyMove {
    enemy.position = ccp(enemy.position.x + x, enemy.position.y);
    enemy2.position = ccp(enemy2.position.x - x2, enemy2.position.y);
    enemy3.position = ccp(enemy3.position.x + x3, enemy3.position.y);

    if (enemy.position.x > 1024 || enemy.position.x < 0) {
        x = -x;
    }
    if (enemy2.position.x > 1024 || enemy2.position.x < 0) {
        x2 = -x2;
    }
    if (enemy3.position.x > 1024 || enemy3.position.x < 0) {
        x3 = -x3;
    }
    if (enemy.position.y > 768 || enemy.position.y < 120) {
        y = -y;
    }

}

-(void) collisionDetection {
    if (CGRectIntersectsRect(player.boundingBox, enemy.boundingBox)) {
        [self schedule:@selector(collisionAlert)];
    }
    if (CGRectIntersectsRect(player.boundingBox, enemy2.boundingBox)) {
        [self schedule:@selector(collisionAlert)];
    }
    if (CGRectIntersectsRect(player.boundingBox, enemy3.boundingBox)) {
        [self schedule:@selector(collisionAlert)];
    }
}

-(void) getStar {
    if (CGRectIntersectsRect(player.boundingBox, star.boundingBox)) {

        NSLog(@"Got Star!");

        scoreNumber += 100;
        NSString *string = [NSString stringWithFormat:@"Score: %i", (int)scoreNumber];
        [score setString:string];

        [self addChild:star2];

        if (star.visible == 1) {

        }
    }
    if (CGRectIntersectsRect(player.boundingBox, star2.boundingBox)) {

        NSLog(@"Got Star!");

        scoreNumber += 100;
        NSString *string = [NSString stringWithFormat:@"Score: %i", (int)scoreNumber];
        [score setString:string];

        [self addChild:star3];
    }
    if (CGRectIntersectsRect(player.boundingBox, star3.boundingBox)) {
        youWin = [CCLabelTTF labelWithString:@"You Win" fontName:@"Marker Felt" fontSize:75];
        youWin.position = ccp(500,400);
        [self addChild:youWin];
        [self setUpWinMenu];
        NSLog(@"Got Star!");


        scoreNumber += 100;
        NSString *string = [NSString stringWithFormat:@"Score: %i", (int)scoreNumber];
        [score setString:string];

        player.position = ccp(player.position.x - 10, player.position.y - 20);
        [self unschedule:@selector(enemyMove)];
        [self unschedule:@selector(collisionAlert)];
        [self unschedule:@selector(getStar)];

    }


    return;
}
-(void) collisionAlert {
    player.position = ccp(player.position.x - 10, player.position.y - 20);
    [self unschedule:@selector(enemyMove)];
    UIAlertView* dialog = [[UIAlertView alloc] init];
    [dialog setDelegate:self];
    [dialog setTitle:@"Fail"];
    [dialog setMessage:@"You are a Failure!"];
    [dialog addButtonWithTitle:@"Goto Main Menu"];
    [dialog addButtonWithTitle:@"Retry!"];
    [dialog addButtonWithTitle:@"Dont push this button"];
    [dialog show];
    [dialog release];
    [self unschedule:@selector(collisionAlert)];
}
-(void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 0) {
        [[CCDirector sharedDirector] replaceScene:[CCTransitionFlipAngular transitionWithDuration:1 scene:[HelloWorldLayer node]]];
    }
    if (buttonIndex == 1) {
        [[CCDirector sharedDirector] replaceScene:[Level_1 node]];
    }
    if (buttonIndex == 2) {

        [self schedule:@selector(noting)];
    }
}

-(void) gotoMainMenu {
    [[CCDirector sharedDirector] replaceScene:[CCTransitionJumpZoom transitionWithDuration:1 scene:[HelloWorldLayer node]]];
}

@end

我还没有完全完成,但可能有一些空方法,但我确信这不是导致问题的原因


所有这些对象:

CCSprite *player;
CCSprite *enemy;
CCSprite *enemy2;
CCSprite *enemy3;
CCSprite *star;
CCSprite *star2;
CCSprite *star3;
CCSprite *bg;
CCSprite *toolBar;

正在使用自动释放方法进行分配,例如CCSprite spriteWithFile:然后,当您以其他方法访问这些对象时,就像您在ccTouchesBegan: withEvent:它们已经被释放了,你得到了EXC_BAD_ACCESS

您可以做的一件事是修复它,那就是调用spriteWithFile:方法后跟一个保留调用,例如

toolBar = [[CCSprite spriteWithFile:@"ToolBar.png"] retain];

但不要忘记释放 Level_1 类上保留的对象dealloc(我没有看到在你的课堂上实现)

-(void) dealloc {

   [toolBar release];
   [super dealloc]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Cocos2d EXC_BAD_ACCESS 的相关文章

随机推荐

  • Android 构建的 ltrace

    伙计们 我正在尝试从这个 repo 构建适用于 Android 的 stracehttps android googlesource com platform external ltrace https android googlesour
  • 更新到 ARC 错误

    我正在尝试将项目更新为 ARC 虽然我看过一些关于更新到 ARC 的帖子 但我见过的帖子都没有处理这个特定问题 我有多个错误 大多数是 ARC Issue Pointer to non const type id with no expli
  • xml 文件未复制到目标 intellij idea

    我在源文件夹中有一些 xml 文件以及 java 文件 Intellij Idea 不会将它们复制到目标文件夹 我希望这些 xml 文件与目标中的类一起出现 Eclipse 做得很好 谁能告诉我如何在 Intellij Idea 中实现这一
  • Java 8 ImageIO 在 Linux 中错误读取 JPEG

    我正在尝试读取用户上传的 JPEG 图像 任意 以在服务器应用程序中创建缩略图 系统在 Windows7 Oracle Java 8u11 中运行良好 但我在服务器上的 CentOS 下遇到颜色模型问题 原始图像是 http studio
  • 如何使用自定义指令作为类在 AngularJS 中使用日期选择器?

    下面是我使用的 HTML 和 Javascript 代码 HTML 代码 div div
  • MySQL 是否有类似 SQL Server TIMESTAMP 列的功能?

    我的初步研究表明 不 我在 SQL Server 中有一个数据库应用程序 它使用 SQL Server 时间戳列来跟踪更改 也就是说 我可以浏览该表并知道 如果我看到时间戳 gt 某个参考时间戳 则该行已被更改添加或更新 我正在寻找 MyS
  • OpenCL 中的障碍

    在 OpenCL 中 我的理解是你可以使用barrier 同步工作组中线程的函数 我 通常 确实了解它们的用途以及何时使用它们 我还知道工作组中的所有线程都必须遇到障碍 否则会出现问题 然而 到目前为止 每次我尝试使用屏障时 似乎都会导致我
  • Highcharts:维恩图如何显示总数和并集数?

    I am using highchart js to generate reports I need to generate venn diagram like the below image 在 的帮助下this post https s
  • 基于 PHP/JavaScript 的安全、可定制、开源聊天引擎,适用于所有流行的浏览器

    请推荐适用于所有流行浏览器的 PHP JavaScript 上的安全 可定制 开源聊天 我自己写的聊天记录
  • 让express.js显示公共文件夹

    我最近使用express js构建了一个快速的单页应用程序 这实际上是我的第一个js框架 实际上 我的第一个js项目 所以我对此非常陌生 我订阅了新的typography com 云字体 但无法找到放置在公共文件夹中的 fonts 文件夹
  • 如何检查Windows文件是否可读/可写?

    首先 我知道这对于实际检查我是否可以写作并不可靠 我正在编写一个文件传输客户端 并且希望 远程 和 本地 文件浏览器窗格之间具有相同的功能 我完全理解 无论如何 我都必须处理执行的任何操作的任何与权限相关的异常 这不是编程检查 只是显示给用
  • 将 TryDequeue 放入 while 循环中安全吗?

    我以前没有使用过并发队列 在 while 循环中使用 TryDequeue 是否可以 难道这事就不能永远陷下去了吗 var cq new ConcurrentQueue
  • 如何在 XML 中创建新行或制表符?

    In my strings xml文件 我有一个很长的文本 我想要格式化 如何在文本的第一句之前添加制表符 另外 新行的代码是什么 Add t对于选项卡和 n对于新线
  • 为什么用函数式语言编写编译器更容易? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我想这个问题很久了 但在 Google 上找不到答案 在 Stackoverflow 上也找不到类似的问题 如果有重复的 我很抱歉 很多人似乎都
  • 使用 vbscript 在命令提示符中执行多个命令

    Set oShell CreateObject WScript Shell oShell Run cmd c c 这条线执行得非常好 现在我需要输入文本 例如 c users gt abcd 我该如何在已经打开的cmd提示符中进行操作 您必
  • 接口类型变量

    我正在学习Java 我看到以下关于界面在一本书中 当变量被声明为接口类型时 它只需 意味着该对象预计已实现该接口 这是什么意思 如果我定义一个界面 public interface Myinterface void method one i
  • 以编程方式查找消息框并生成按钮点击

    我正在尝试自动测试 winform 应用程序 我在与测试代码相同的进程中运行它 因此很容易找到 Net 控件并在它们上模拟用户操作 然而 我遇到了一个消息框 使用标准 MessageBox Show 方法创建 的问题 我怎样才能掌握它并模拟
  • 如何在执行 Environment.Exit() 之前刷新 dotnet 核心应用程序中的所有记录器

    基本上 我遇到了应用程序崩溃并且没有调试日志的老问题 因为应用程序在写入日志之前就终止了 经典的方法是捕获 main 中的所有异常并刷新异常块中的所有日志缓冲区 此外 添加睡眠以降低重新启动速度并允许缓冲区完成刷新 我使用serilog 我
  • 使用 jsonb_set() 更新特定的 jsonb 数组值

    目前我正在使用 PostgreSQL 9 5 并尝试更新 jsonb 字段数组内的值 但我无法获取所选值的索引 我的桌子看起来像这样 CREATE TABLE samples id serial sample jsonb 我的 JSON 看
  • Cocos2d EXC_BAD_ACCESS

    我是 cocos2d 的新手 突然得到了这个 EXC BAD ACCESS 我做了一个新的获胜菜单 但出现了错误 我认为错误是因为我调用了一个已释放的对象 但我没有释放任何东西 我的调试控制台没有错误 这很奇怪 这是我的 Level 1 m