Here is a perplexing problem I am having, any help would be greatly appreciated as this is becoming an urgent issue for me. I have a basic tile memory game that currently has only one audio attachment when the 2 tiles are matched. What I need to do is add a different audio effect to each tile eg. 'trumpet note for trumpet tile' as well as a specific audio effect when the two correct tiles are matched eg: 2sec trumpet music. Here is my code:
//
// memoryGame.m
// LahLah_Book1
//
// Created by Sharp Agency on 5/02/2015.
// Copyright 2015 __MyCompanyName__. All rights reserved.
//
#import "memoryGame.h"
#import "SimpleAudioEngine.h"
#import "bandhouse.h"
//#import "CocosDenshion.h"
@implementation memorygame
@synthesize emitter;
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
memorygame *layer = [memorygame 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])) {
CCLOG(@"initialising memorygame layer");
CGSize screenSize = [[CCDirector sharedDirector] winSize];
//// here is the audio loadup for the background music:
[[SimpleAudioEngine sharedEngine] playBackgroundMusic: @"red.mp3" loop:YES];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"boing_good copy.mp3"];
// [[SimpleAudioEngine sharedEngine] preloadEffect:@"ahh.wav"];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"beep-29.mp3"];
CCSprite* theBackground = [CCSprite spriteWithFile:@"soundcupboardscreen.jpg"];
theBackground.position = ccp (screenSize.width / 2 , screenSize.height / 2);
[self addChild:theBackground z:-0];
self.isTouchEnabled = YES;
// Initilize image touch count
imageTouchCount = 0;
// Initialise tag variables
oldTag = 0;
newTag = 0;
// Initialize score
score = 0;
CCMenuItem *button4 = [CCMenuItemImage itemFromNormalImage:@"homebut1.png" selectedImage:@"homebut2.png" target:
self selector:@selector(gotoBandhouse)];
CCMenu *Menu4 = [CCMenu menuWithItems: button4, nil];
Menu4.position = ccp(screenSize.width /19 , screenSize.height /1.07);
[self addChild:Menu4 z:4];
/////////////////////////Game settings///////////////////////////////////////////////////////
// Initiate image array with image names
imageArray = [[NSArray alloc] initWithObjects:
@"banjo",
@"guitar",
@"congos",
@"doublebase",
@"panpipes",
@"xylophone",
@"triangle",
@"trombone",
@"trumpet",
@"clarinet",
nil];
// Initialise image tag counter
for (int k = 0; k<10; k++)
{
imagetagcounter [k] = 0;
}
// Initiate location variables
x = 170;
y = 120;
// create sprites to hide images
for (int i = 0; i<4; i++)
{
for (int j = 0; j<5; j++)
{
// Randomly select an image array index and make a related tag for image sprite
int random;
// Make non repeating random value. (Repeat exactly two times because two sprites share same image
do
{
random = arc4random()%10;
} while (imagetagcounter[random]==2);
CCSprite*imageSprite = [CCSprite spriteWithFile:@"frontcard.png"];
imageSprite.position = ccp (x,y);
// we have to create two related tags random and random+10
imageSprite.tag = random + (imagetagcounter[random])*10;
[self addChild:imageSprite z:2];
// Increment image tag counter
imagetagcounter[random]++;
// Increment x position value
x = x+170;
}
// Increment y position value
y = y+160;
// Reset x position value at the start of a new row
x = 170;
}
// Add score label
scoreLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%d",score] fontName:@"Marker Felt"
fontSize:36];
scoreLabel.position = ccp(40,40);
[self addChild:scoreLabel];
}
return self;
}
- (void) gotoBandhouse {
// [[SimpleAudioEngine sharedEngine]playEffect:@"page-flip-8.mp3"];
CCTransitionFade* transition = [CCTransitionFade transitionWithDuration:1 scene: [bandhouse scene]];
[[CCDirector sharedDirector] replaceScene:transition];
}
- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touchPoint = [touches anyObject];
CGPoint location = [touchPoint locationInView: [touchPoint view]];
location = [[CCDirector sharedDirector] convertToGL:location];
//iterate through 20 sprites two get the touchedsprite
for (int l = 0; l<20; l++)
{
CCSprite *touchedSprite = (CCSprite *) [self getChildByTag:l];
// [[SimpleAudioEngine sharedEngine]playEffect:@"trash.mp3"];
//
// [[SimpleAudioEngine sharedEngine]setEffectsVolume:(0.5)];
// [[SimpleAudioEngine sharedEngine]playEffect:@"beep-29.mp3"];
//compare touch point with current sprite
if (CGRectContainsPoint([touchedSprite boundingBox], location))
{
NSLog(@"Image tag = %d",l);
// Get image index from tag
int imgIndex;
if (l<10)
{
imgIndex = l;
}
else
{
imgIndex = l-10;
}
//replace sprite image with original image
[touchedSprite setTexture:[[CCTextureCache sharedTextureCache]addImage:[NSString stringWithFormat:@"%@.png"
,[imageArray objectAtIndex:imgIndex]]]];
// Increment image touch count
imageTouchCount++;
if (imageTouchCount == 2)
{
//Disable touch for 2 seconds to avoid change in touch tag
self.isTouchEnabled = NO;
//Second opened sprite tag
newTag = l;
// Compare new tag with old tag
if (newTag == oldTag-10 || newTag == oldTag+10)
{
[self performSelector:@selector(sameImageMethod) withObject:nil afterDelay:1];
}
else
{
[self performSelector:@selector(resetImages) withObject:nil afterDelay:2];
}
imageTouchCount = 0;
}
else if (imageTouchCount == 1)
{
//first opened sprite tag
oldTag = l;
}
break;
}
}
}
- (void) resetImages
{
//Get both opened images
CCSprite *openSprite1 = (CCSprite *) [self getChildByTag:oldTag];
CCSprite *openSprite2 = (CCSprite *) [self getChildByTag:newTag];
//Reset two open images
[openSprite1 setTexture:[[CCTextureCache sharedTextureCache]addImage:@"frontcard.png"]];
[openSprite2 setTexture:[[CCTextureCache sharedTextureCache]addImage:@"frontcard.png"]];
[[SimpleAudioEngine sharedEngine]playEffect:@"boing_good copy.mp3"];
// Enable touch
self.isTouchEnabled = YES;
}
-(void) sameImageMethod
{
CCSprite *openSprite1 = (CCSprite *) [self getChildByTag:oldTag];
CCSprite *openSprite2 = (CCSprite *) [self getChildByTag:newTag];
[openSprite1 runAction:[CCScaleTo actionWithDuration:1 scale:0]];
[openSprite2 runAction:[CCScaleTo actionWithDuration:1 scale:0]];
[[SimpleAudioEngine sharedEngine]playEffect:@"new choose me.mp3"];
// Increment score and update score label
score = score+10;
[scoreLabel setString:[NSString stringWithFormat:@"%d", score]];
if (score == 100)
{
// CGSize screenSize = [[CCDirector sharedDirector] winSize];
emitter = [CCParticleSystemQuad particleWithFile:@"starbang_blue.plist"];
emitter.position = ccp(140, 160);
[self addChild:emitter z:10];
emitter = [CCParticleSystemQuad particleWithFile:@"starbang_yellow.plist"];
emitter.position = ccp(300, 300);
[self addChild:emitter z:10];
emitter = [CCParticleSystemQuad particleWithFile:@"starbang_pink.plist"];
emitter.position = ccp(800, 450);
[self addChild:emitter z:10];
emitter = [CCParticleSystemQuad particleWithFile:@"starbang_blue.plist"];
emitter.position = ccp(600, 200);
[self addChild:emitter z:10];
emitter = [CCParticleSystemQuad particleWithFile:@"starbang_green.plist"];
emitter.position = ccp(800, 200);
[self addChild:emitter z:10];
emitter = [CCParticleSystemQuad particleWithFile:@"starbang_pink.plist"];
emitter.position = ccp(50, 800);
[self addChild:emitter z:10];
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Excellent!" message:@"Your awesome!" delegate:self
// cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
// [alert show];
[[SimpleAudioEngine sharedEngine]playEffect:@"ahh.wav"];
}
// Enable touch
self.isTouchEnabled = YES;
}
- (void)alertview:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Go to home page
// [self toHome];
}
// 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"
[super dealloc];
}
@end
Aucun commentaire:
Enregistrer un commentaire