1

我似乎无法解决这个错误!

我正在做一个小项目,试图将背景音频添加到我的应用程序中。在出现此错误之前,我做得很好。

.m 文件的第 52 行出错:

//
//  UDMenuScene.m
//  iLabyrinth
//
//  Created by Rolandas Razma on 5/12/10.
//  Copyright 2010 UD7. All rights reserved.
//

#import "UDMenuScene.h"
#import "UDPickMapScene.h"
#import "SimpleAudioEngine.h"
#import "UDHelpScene.h"
#import "iLabyrinth.h"
#import "CCDirector.h"

@implementation UDMenuScene
#pragma mark -
#pragma mark NSObject

- (id)init 
{
    if( (self = [super init]) )
    {
         [self addChild:[UDMenuLayer node]];
    }
    return self;
}

@end

@implementation UDMenuLayer
#pragma mark -
#pragma mark NSObject

- (id)init {

    if( (self = [super init]) )
    {
        [self setIsTouchEnabled: YES];
        CGSize _winSize = [[CCDirector sharedDirector] winSize];
        //AVAudioPlayer *player;
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"menu" ofType:@"mp3"]];
        NSError *error;
        player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        // Add background
        CCSprite *sprite = [CCSprite spriteWithFile:[NSString stringWithFormat:@"menu%@.png", (isDeviceIPad()?@"~ipad":([iLabyrinth hightRes]?@"@2x":@""))]];
        [sprite setAnchorPoint:CGPointZero];
        [self addChild:sprite];
        _backgroundLayer = [CCSpriteBatchNode batchNodeWithFile:[iLabyrinth textureName]];
        [[_backgroundLayer texture] setAliasTexParameters];
        [self addChild:_backgroundLayer];
        // Add "Pay" button
        CCSprite *playSprite = [CCSprite spriteWithSpriteFrameName:@"play.png"];
            [playSprite setTag:MenuButtonPlay];
            [playSprite setAnchorPoint:CGPointMake(0.5f, 0.0f)];
            [playSprite setPosition:CGPointMake(_winSize.width /2, _winSize.height /2)];
        [_backgroundLayer addChild:playSprite];

        // Add "Help" button        
        CCSprite *helpSprite = [CCSprite spriteWithSpriteFrameName:@"help.png"];
            [helpSprite setAnchorPoint:CGPointMake(0.5f, 1.0f)];
            [helpSprite setTag:MenuButtonHelp];
            [helpSprite setPosition:CGPointMake(_winSize.width /2, _winSize.height /2 -[helpSprite boundingBox].size.height /2)];
        [_backgroundLayer addChild:helpSprite];
    }

    return self;
}


#pragma mark -
#pragma mark CCStandardTouchDelegate


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

    CGPoint touchPoint = [_backgroundLayer convertTouchToNodeSpace:[touches anyObject]];

    for( CCSprite *sprite in [_backgroundLayer children] ){
        if( [sprite tag] <= 0 ) continue;

        if( CGRectContainsPoint([sprite boundingBox], touchPoint) ){

            [sprite setColor:ccGREEN];
            [[SimpleAudioEngine sharedEngine] playEffect:@"click.caf"];

            switch ( [sprite tag] ) {
                case MenuButtonPlay: {
                    [[CCDirector sharedDirector] replaceScene:[CCTransitionSplitRows transitionWithDuration:1.0f scene:[UDPickMapScene node]]];
                    break;
                }
                case MenuButtonHelp: {
                    [[CCDirector sharedDirector] replaceScene:[CCTransitionSplitRows transitionWithDuration:1.0f scene:[UDHelpScene node]]];
                    break;
                }
            }

            return;
        }

    }

}


@end

这是我的 .h 文件:

//
//  UDMenuScene.h
//  iLabyrinth
//
//  Created by Rolandas Razma on 5/12/10.
//  Copyright 2010 UD7. All rights reserved.
//

#import "cocos2d.h"
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

typedef enum {
    MenuButtonPlay =1,
    MenuButtonHelp,
    MenuButtonPlayHeaven,
} MenuButton;

@class player;

@interface UDMenuScene : CCScene {

}
@property (nonatomic, retain) AVAudioPlayer *player;
@end

@interface UDMenuLayer : CCLayer {
    CCSpriteBatchNode   *_backgroundLayer;
}
@end
4

1 回答 1

6

您正在声明一个名为player.

@class player;

(前向声明说player是类类型)

但您也player用作标识符

@property (nonatomic, retain) AVAudioPlayer *player;

(属性声明player是 的一个实例AVAudioPlayer)。

为了防止类似的错误,永远不要声明名称以小写字母开头的类(Player改用),并使用自定义前缀来避免名称冲突(例如MYPlayer)。

于 2013-11-07T10:10:02.250 回答