In my iOS app I've to do an action after a delay. I will explain it better: my app can recognize some audio frequency:
18100 Hz
18200 Hz
18300 Hz
18500 Hz
To do that I've modified the pitch detector. I want to start a timer when the device doesn't recognize one of the frequency that I've setter up in my app.
I made so:
- (void)frequencyChangedWithValue:(float)newFrequency {
frequencyRecived = newFrequency;
watermarkReceived = YES;
if (frequencyRecived > 18000) {
if (frequencyRecived >= 18000 && frequencyRecived <= 18110 && !water1) {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"1"];
water2 = water3 = water4 = NO;
water1 = YES;
}
if (frequencyRecived >= 18115 && frequencyRecived <= 18250 && !water2) {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"2"];
water1 = water3 = water4 = NO;
water2 = YES;
}
if (frequencyRecived >= 18255 && frequencyRecived <= 18440 && !water3) {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"3"];
water1 = water2 = water4 = NO;
water3 = YES;
}
if (frequencyRecived >= 18450 && !water4) {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"4"];
water1 = water2 = water3 = NO;
water4 = YES;
}
} else {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"Nessuna postazione"];
water1 = water2 = water3 = water4 = NO;
}
}
In the else section I've to start a timer that it count for 10 seconds, after this time I've to write a label. I tried to use the following solution:
[self performSelector:<#(SEL)#> withObject:<#(id)#> afterDelay:<#(NSTimeInterval)#> inModes:<#(NSArray *)#>]
, but it never call the selector method because every time the app execute the code in else it restart the timer.
Can you suggest me a solution to solve this issue?
Thanks
UPDATE
I follow the answer, but it doesn't work. I post here the code updated:
#interface ViewController () {
BOOL watermarkReceived;
float frequencyRecived;
CABasicAnimation *theAnimation;
BOOL water1, water2, water3, water4;
}
#property(nonatomic,strong)NSTimer *timer;
#end
- (void)frequencyChangedWithValue:(float)newFrequency {
frequencyRecived = newFrequency;
watermarkReceived = YES;
if (frequencyRecived > 18000) {
[self.timer invalidate];
self.timer = nil;
if (frequencyRecived >= 18000 && frequencyRecived <= 18110 && !water1) {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"1"];
water2 = water3 = water4 = NO;
water1 = YES;
}
if (frequencyRecived >= 18115 && frequencyRecived <= 18250 && !water2) {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"2"];
water1 = water3 = water4 = NO;
water2 = YES;
}
if (frequencyRecived >= 18255 && frequencyRecived <= 18440 && !water3) {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"3"];
water1 = water2 = water4 = NO;
water3 = YES;
}
if (frequencyRecived >= 18450 && !water4) {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"4"];
water1 = water2 = water3 = NO;
water4 = YES;
}
} else {
self.timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(noPosition) userInfo:nil repeats:NO];
// [self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"Nessuna postazione"];
water1 = water2 = water3 = water4 = NO;
}
}
- (void)noPosition {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"Nessuna postazione"];
}
PS: I understood the other problems you told me and I will solve in the future, for now I need to fix the timer issue. :)
Use an NSTimer. In the else branch, check if the timer already exists and, if not, create and schedule it. In the main branch of the if, invalidate and destroy the timer.
Also, your if statement has a number of holes and is overly complex. You have already checked that the frequency is > 18000 so you don't need to check it again. You are also using if, else if to simplify because each is a distinct range, and if one range is detected then none of the other ranges can be. In this way you can just check that the upper range bound is not exceeded in the inner if.
Also, setting the text in the background looks wrong. UI updates need to be made from the main thread
Something like:
- (void)frequencyChangedWithValue:(float)newFrequency {
frequencyRecived = newFrequency;
watermarkReceived = YES;
if (frequencyRecived > 18000) {
[self.timer invalidate];
self.timer = nil;
if (frequencyRecived <= 18110) {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"1"];
water2 = water3 = water4 = NO;
water1 = YES;
}
else if (frequencyRecived <= 18250) {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"2"];
water1 = water3 = water4 = NO;
water2 = YES;
}
else if (frequencyRecived <= 18440) {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"3"];
water1 = water2 = water4 = NO;
water3 = YES;
}
else {
[self performSelectorInBackground:#selector(setTextInLabel:) withObject:#"4"];
water1 = water2 = water3 = NO;
water4 = YES;
}
} else {
if (self.timer == nil) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:... target:self selector:... userInfo:nil repeats:NO];
}
water1 = water2 = water3 = water4 = NO;
}
}
Related
Hi iam really new to coding and to start of i am making a really simple game in Xcode. The game is done but i do have a little problem. rarely at times when in game over pig.image does not change and the previous animation keeps on going even after [pig stopAnimation]... heres the code from when you get game over to where the image should change.....
-(void)gameover1{
[jetpacksound stop];
[self hitsound];
[pig stopAnimating];
[movementtimer invalidate];
if (score > highscorenumber) {
highscorenumber = score;
[[NSUserDefaults standardUserDefaults] setInteger:highscorenumber forKey:#"highscoresaved"];
}
else{
highscore.text = [NSString stringWithFormat:#"Try Harder Next Time"];
}
movementtimer = [NSTimer scheduledTimerWithTimeInterval:.035 target:self selector:#selector(gameover1movement) userInfo:nil repeats:YES];
bam.hidden = NO;
flash.hidden = NO;
background1.hidden = YES;
background2.hidden = YES;
background3.hidden = YES;
pauseg.hidden = YES;
scorelabel.hidden = YES;
fork1.hidden = YES;
fork2.hidden = YES;
fork3.hidden = YES;
fork4.hidden = YES;
knife1.hidden = YES;
knife2.hidden = YES;
knife3.hidden = YES;
knife4.hidden = YES;
poisoncloud.hidden = YES;
cake.hidden = YES;
}
-(void)gameover1movement{
pig.center = CGPointMake(pig.center.x, pig.center.y - 30);
if (IS_IPAD)
{
//do stuff for iPad
if (pig.center.y < 400) {
[self gameover2];
}
}
else
{
if(IS_IPHONE_5)
{
//do stuff for 4 inch iPhone screen
if (pig.center.y < 200) {
[self gameover2];
}
}
else
{
//do stuff for 3.5 inch iPhone screen
}
}
}
-(void)gameover2{
[movementtimer invalidate];
movementtimer = [NSTimer scheduledTimerWithTimeInterval:.035 target:self selector:#selector(gameover2movement) userInfo:nil repeats:YES];
pig.image = [UIImage imageNamed:#"DeadPig.png"];
background1.hidden = NO;
background2.hidden = NO;
background3.hidden = NO;
bam.hidden = YES;
flash.hidden = YES;
if (IS_IPAD)
{
//do stuff for iPad
pig.frame = CGRectMake(pig.center.x - 115, pig.center.y - 165, 230.0f, 330.0f);
if (pig.center.x < 115) {
pig.center = CGPointMake(115, pig.center.y);
}
if (pig.center.x > 653) {
pig.center = CGPointMake(653, pig.center.y);
}
background3.center = CGPointMake(384, 512);
background2.center = CGPointMake(384, 1536);
background1.center = CGPointMake(384, 2560);
}
else
{
if(IS_IPHONE_5)
{
//do stuff for 4 inch iPhone screen
pig.frame = CGRectMake(pig.center.x - 50, pig.center.y - 75, 100.0f, 150.0f);
if (pig.center.x < 50) {
pig.center = CGPointMake(50, pig.center.y);
}
if (pig.center.x > 270) {
pig.center = CGPointMake(270, pig.center.y);
}
background3.center = CGPointMake(160, 284);
background2.center = CGPointMake(160, 852);
background1.center = CGPointMake(160, 1420);
}
else
{
//do stuff for 3.5 inch iPhone screen
}
}
}
Here is the Code I am currently using. If there is anything you need clarified I will do it as soon as I can. I have asked questions about other things as well that seemed to broad so I put every piece of code I am using related to the switch of views minus the basic stuff such as viewDidLoad.
Please read the following and answer/ comment as you see fit.
Thank you for the help.
-(IBAction)startgame:(id)sender
{
// load the game screen
if(self.gamescreenViewController == nil)
{
GameScreen *gameController = [[GameScreen alloc] initWithNibName:#"GameScreen" bundle:nil];
self.gamescreenViewController = gameController;
[gameController release];
}
// code to animate the switch
[UIView beginAnimations:#"View Flip" context:nil];
[UIView setAnimationDuration:1.25];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// load Ultimate tic tac toe view
if(self.ultimateViewController.view.superview == nil)
{
// second code to animate switch
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
// [UIView setAnimationTransition: UIViewAnimationOptionTransitionCurlDown forView:self.view cache:YES];
// show the ultimate view controller
[ultimateViewController viewWillAppear:YES];
[gamescreenViewController viewWillDisappear:YES];
[gamescreenViewController.view removeFromSuperview];
[self.view insertSubview:ultimateViewController.view atIndex:0];
[gamescreenViewController viewDidDisappear:YES];
[ultimateViewController viewDidAppear:YES];
// change button image
UIImage *buttonImage = [UIImage imageNamed:#"StartGame.png"];
[switchbutton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:switchbutton];
// setting values to zero after switch buttton action
int symb1 = [[self.ultimateViewController.Symbolfield1 text] intValue];
int symb2 = [[self.ultimateViewController.Symbolfield2 text] intValue];
int symb3 = [[self.ultimateViewController.Symbolfield3 text] intValue];
int turns = [[self.gamescreenViewController.turns text] intValue];
symb1 = 0;
symb2 = 0;
symb3 = 0;
turns = 1;
[ultimateViewController.Symbolfield1 setText:[NSString stringWithFormat:#"%1i", symb1]];
[ultimateViewController.Symbolfield2 setText:[NSString stringWithFormat:#"%1i", symb2]];
[ultimateViewController.Symbolfield3 setText:[NSString stringWithFormat:#"%1i", symb3]];
[gamescreenViewController.turns setText:[NSString stringWithFormat:#"%1i", turns]];
// endable buttons after switch button
ultimateViewController.a1.enabled = YES;
ultimateViewController.a2.enabled = YES;
ultimateViewController.a3.enabled = YES;
ultimateViewController.a4.enabled = YES;
ultimateViewController.a5.enabled = YES;
ultimateViewController.a6.enabled = YES;
ultimateViewController.a7.enabled = YES;
ultimateViewController.a8.enabled = YES;
ultimateViewController.a9.enabled = YES;
ultimateViewController.a10.enabled = YES;
ultimateViewController.a11.enabled = YES;
ultimateViewController.a12.enabled = YES;
ultimateViewController.b1.enabled = YES;
ultimateViewController.b2.enabled = YES;
ultimateViewController.b3.enabled = YES;
ultimateViewController.b4.enabled = YES;
ultimateViewController.b5.enabled = YES;
ultimateViewController.b6.enabled = YES;
ultimateViewController.b7.enabled = YES;
ultimateViewController.b8.enabled = YES;
ultimateViewController.b9.enabled = YES;
ultimateViewController.b10.enabled = YES;
ultimateViewController.b11.enabled = YES;
ultimateViewController.b12.enabled = YES;
ultimateViewController.c1.enabled = YES;
ultimateViewController.c2.enabled = YES;
ultimateViewController.c3.enabled = YES;
ultimateViewController.c4.enabled = YES;
ultimateViewController.c5.enabled = YES;
ultimateViewController.c6.enabled = YES;
ultimateViewController.c7.enabled = YES;
ultimateViewController.c8.enabled = YES;
ultimateViewController.c9.enabled = YES;
ultimateViewController.c10.enabled = YES;
ultimateViewController.c11.enabled = YES;
ultimateViewController.c12.enabled = YES;
gamescreenViewController.move1.enabled = YES;
gamescreenViewController.move2.enabled = YES;
gamescreenViewController.move3.enabled = YES;
gamescreenViewController.move4.enabled = YES;
gamescreenViewController.move5.enabled = YES;
gamescreenViewController.move6.enabled = YES;
gamescreenViewController.move7.enabled = YES;
gamescreenViewController.move8.enabled = YES;
gamescreenViewController.move9.enabled = YES;
gamescreenViewController.move10.enabled = YES;
gamescreenViewController.move11.enabled = YES;
gamescreenViewController.move12.enabled = YES;
gamescreenViewController.move13.enabled = YES;
gamescreenViewController.move14.enabled = YES;
gamescreenViewController.move15.enabled = YES;
gamescreenViewController.move16.enabled = YES;
gamescreenViewController.move17.enabled = YES;
gamescreenViewController.move18.enabled = YES;
gamescreenViewController.move19.enabled = YES;
gamescreenViewController.move20.enabled = YES;
gamescreenViewController.move21.enabled = YES;
gamescreenViewController.move22.enabled = YES;
gamescreenViewController.move23.enabled = YES;
gamescreenViewController.move24.enabled = YES;
gamescreenViewController.move25.enabled = YES;
gamescreenViewController.move26.enabled = YES;
gamescreenViewController.move27.enabled = YES;
gamescreenViewController.move28.enabled = YES;
gamescreenViewController.move29.enabled = YES;
gamescreenViewController.move30.enabled = YES;
gamescreenViewController.move31.enabled = YES;
gamescreenViewController.move32.enabled = YES;
gamescreenViewController.move33.enabled = YES;
gamescreenViewController.move34.enabled = YES;
gamescreenViewController.move35.enabled = YES;
gamescreenViewController.move36.enabled = YES;
gamescreenViewController.move37.enabled = YES;
gamescreenViewController.move38.enabled = YES;
gamescreenViewController.move39.enabled = YES;
gamescreenViewController.move40.enabled = YES;
gamescreenViewController.move41.enabled = YES;
gamescreenViewController.move42.enabled = YES;
gamescreenViewController.move43.enabled = YES;
gamescreenViewController.move44.enabled = YES;
gamescreenViewController.move45.enabled = YES;
gamescreenViewController.move46.enabled = YES;
gamescreenViewController.move47.enabled = YES;
gamescreenViewController.move48.enabled = YES;
gamescreenViewController.move49.enabled = YES;
gamescreenViewController.move50.enabled = YES;
gamescreenViewController.move51.enabled = YES;
gamescreenViewController.move52.enabled = YES;
gamescreenViewController.move53.enabled = YES;
gamescreenViewController.move54.enabled = YES;
gamescreenViewController.move55.enabled = YES;
gamescreenViewController.move56.enabled = YES;
gamescreenViewController.move57.enabled = YES;
gamescreenViewController.move58.enabled = YES;
gamescreenViewController.move59.enabled = YES;
gamescreenViewController.move60.enabled = YES;
gamescreenViewController.move61.enabled = YES;
gamescreenViewController.move62.enabled = YES;
gamescreenViewController.move63.enabled = YES;
gamescreenViewController.move64.enabled = YES;
gamescreenViewController.move65.enabled = YES;
gamescreenViewController.move66.enabled = YES;
gamescreenViewController.move67.enabled = YES;
gamescreenViewController.move68.enabled = YES;
gamescreenViewController.move69.enabled = YES;
gamescreenViewController.move70.enabled = YES;
gamescreenViewController.move71.enabled = YES;
gamescreenViewController.move72.enabled = YES;
gamescreenViewController.move73.enabled = YES;
gamescreenViewController.move74.enabled = YES;
gamescreenViewController.move75.enabled = YES;
gamescreenViewController.move76.enabled = YES;
gamescreenViewController.move77.enabled = YES;
gamescreenViewController.move78.enabled = YES;
gamescreenViewController.move79.enabled = YES;
gamescreenViewController.move80.enabled = YES;
gamescreenViewController.move81.enabled = YES;
// clear images after switch button action
gamescreenViewController.image1a.image = NO;
gamescreenViewController.image2a.image = NO;
gamescreenViewController.image3a.image = NO;
gamescreenViewController.image4a.image = NO;
gamescreenViewController.image5a.image = NO;
gamescreenViewController.image6a.image = NO;
gamescreenViewController.image7a.image = NO;
gamescreenViewController.image8a.image = NO;
gamescreenViewController.image9a.image = NO;
gamescreenViewController.image1b.image = NO;
gamescreenViewController.image2b.image = NO;
gamescreenViewController.image3b.image = NO;
gamescreenViewController.image4b.image = NO;
gamescreenViewController.image5b.image = NO;
gamescreenViewController.image6b.image = NO;
gamescreenViewController.image7b.image = NO;
gamescreenViewController.image8b.image = NO;
gamescreenViewController.image9b.image = NO;
gamescreenViewController.image1c.image = NO;
gamescreenViewController.image2c.image = NO;
gamescreenViewController.image3c.image = NO;
gamescreenViewController.image4c.image = NO;
gamescreenViewController.image5c.image = NO;
gamescreenViewController.image6c.image = NO;
gamescreenViewController.image7c.image = NO;
gamescreenViewController.image8c.image = NO;
gamescreenViewController.image9c.image = NO;
gamescreenViewController.image1d.image = NO;
gamescreenViewController.image2d.image = NO;
gamescreenViewController.image3d.image = NO;
gamescreenViewController.image4d.image = NO;
gamescreenViewController.image5d.image = NO;
gamescreenViewController.image6d.image = NO;
gamescreenViewController.image7d.image = NO;
gamescreenViewController.image8d.image = NO;
gamescreenViewController.image9d.image = NO;
gamescreenViewController.image1e.image = NO;
gamescreenViewController.image2e.image = NO;
gamescreenViewController.image3e.image = NO;
gamescreenViewController.image4e.image = NO;
gamescreenViewController.image5e.image = NO;
gamescreenViewController.image6e.image = NO;
gamescreenViewController.image7e.image = NO;
gamescreenViewController.image8e.image = NO;
gamescreenViewController.image9e.image = NO;
gamescreenViewController.image1f.image = NO;
gamescreenViewController.image2f.image = NO;
gamescreenViewController.image3f.image = NO;
gamescreenViewController.image4f.image = NO;
gamescreenViewController.image5f.image = NO;
gamescreenViewController.image6f.image = NO;
gamescreenViewController.image7f.image = NO;
gamescreenViewController.image8f.image = NO;
gamescreenViewController.image9f.image = NO;
gamescreenViewController.image1g.image = NO;
gamescreenViewController.image2g.image = NO;
gamescreenViewController.image3g.image = NO;
gamescreenViewController.image4g.image = NO;
gamescreenViewController.image5g.image = NO;
gamescreenViewController.image6g.image = NO;
gamescreenViewController.image7g.image = NO;
gamescreenViewController.image8g.image = NO;
gamescreenViewController.image9g.image = NO;
gamescreenViewController.image1h.image = NO;
gamescreenViewController.image2h.image = NO;
gamescreenViewController.image3h.image = NO;
gamescreenViewController.image4h.image = NO;
gamescreenViewController.image5h.image = NO;
gamescreenViewController.image6h.image = NO;
gamescreenViewController.image7h.image = NO;
gamescreenViewController.image8h.image = NO;
gamescreenViewController.image9h.image = NO;
gamescreenViewController.image1i.image = NO;
gamescreenViewController.image2i.image = NO;
gamescreenViewController.image3i.image = NO;
gamescreenViewController.image4i.image = NO;
gamescreenViewController.image5i.image = NO;
gamescreenViewController.image6i.image = NO;
gamescreenViewController.image7i.image = NO;
gamescreenViewController.image8i.image = NO;
gamescreenViewController.image9i.image = NO;
gamescreenViewController.playerturnimage.image = NO;
gamescreenViewController.playerturnlabel.text = #"Your Turn Player One";
}
else
{
// load game screen view
if(self.gamescreenViewController.view.superview == nil)
{
int symb1 = [[self.ultimateViewController.Symbolfield1 text] intValue];
int symb2 = [[self.ultimateViewController.Symbolfield2 text] intValue];
int symb3 = [[self.ultimateViewController.Symbolfield3 text] intValue];
if ((symb1 < 1) || (symb2 <1) || (symb3 <1))
{
// warning message
UIAlertView *alertview1 = [[ UIAlertView alloc]
initWithTitle:#"No Symbols Selected"
message:#"\n You must select an \n image for each player \n\n"
delegate:nil cancelButtonTitle:#"Got it"
otherButtonTitles: nil];
[alertview1 show];
[alertview1 release];
}
else
{
// show the game screen
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
// show the game controller
[gamescreenViewController viewWillAppear:YES];
[ultimateViewController viewWillDisappear:YES];
[ultimateViewController.view removeFromSuperview];
[self.view insertSubview:gamescreenViewController.view atIndex:0];
[ultimateViewController viewDidDisappear:YES];
[gamescreenViewController viewDidAppear:YES];
// change button image
UIImage *buttonImage = [UIImage imageNamed:#"EndGame.png"];
[switchbutton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:switchbutton];
// hides and disables the undo button for the first move
gamescreenViewController.undobutton.enabled = NO;
gamescreenViewController.undobutton.hidden = YES;
}
// send values from ultimate viewcontroller to gamescreen
int playerpick = [[self.ultimateViewController.SegmentChange text] intValue];
[self.gamescreenViewController.SegmentChange2a setText:[NSString stringWithFormat:#"%1i",playerpick]];
int symbol1 = [[self.ultimateViewController.Symbolfield1 text] intValue];
[self.gamescreenViewController.Symbolfield1a setText:[NSString stringWithFormat:#"%1i",symbol1]];
int symbol2 = [[self.ultimateViewController.Symbolfield2 text] intValue];
[self.gamescreenViewController.Symbolfield2a setText:[NSString stringWithFormat:#"%1i",symbol2]];
int symbol3 = [[self.ultimateViewController.Symbolfield3 text] intValue];
[self.gamescreenViewController.Symbolfield3a setText:[NSString stringWithFormat:#"%1i",symbol3]];
}
}
// statement to enact switch animation
[UIView commitAnimations];
}
Keep in mind that this is set to a certain game I am making as an action connected to the start button.
Any suggestions?
It looks like you aren't actually presenting the view that you are loading from the nib.
*This is the whole code :)
I have been trying to fix this for an hour now, but i can still not make it.
I would be happy if someone could help me :)
Still can't make the UI_USER_INTERFACE_IDIOM code work.*
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(void)Collision{
if (CGRectIntersectsRect(Heli.frame, Obstacle.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Obstacle2.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Bottom1.frame)) {
[self EndGame];
}
if (CGRectIntersectsRect(Heli.frame, Top1.frame)) {
[self EndGame];
}
}
-(void)EndGame{
if (Scorenumber > HighScore) {
HighScore = Scorenumber;
[[NSUserDefaults standardUserDefaults] setInteger:HighScore
forKey:#"HighScoreSaved"];
}
Heli.hidden = YES;
[timer invalidate];
[Scorer invalidate];
[self performSelector:#selector(NewGame) withObject: nil afterDelay:2];
}
-(void)NewGame{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
// For iPhone
Bottom1.hidden = YES;
Top1.hidden = YES;
Obstacle.hidden = YES;
Obstacle2.hidden = YES;
corona.hidden = YES;
Intro1.hidden = NO;
Intro2.hidden = NO;
Intro3.hidden = NO;
Heli.hidden = NO;
Heli.center = CGPointMake(88, 286);
Heli.image = [UIImage imageNamed:#"buss til app opp.png"];
Start = YES;
Scorenumber = 0;
Score.text = [NSString stringWithFormat:#"Score: 0"];
Intro3.text = [NSString stringWithFormat:#"HighScore: %i", HighScore];
}
} else{
// For iPad
Bottom1.hidden = YES;
Top1.hidden = YES;
Obstacle.hidden = YES;
Obstacle2.hidden = YES;
corona.hidden = YES;
Intro1.hidden = NO;
Intro2.hidden = NO;
Intro3.hidden = NO;
Heli.hidden = NO;
Heli.center = CGPointMake(153, 515);
Heli.image = [UIImage imageNamed:#"buss til app opp.png"];
Start = YES;
Scorenumber = 0;
Score.text = [NSString stringWithFormat:#"Score: 0"];
Intro3.text = [NSString stringWithFormat:#"HighScore: %i", HighScore];
Just look at following code:
-(void)NewGame{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
// For iPhone
Bottom1.hidden = YES;
Top1.hidden = YES;
Obstacle.hidden = YES;
Obstacle2.hidden = YES;
corona.hidden = YES;
Intro1.hidden = NO;
Intro2.hidden = NO;
Intro3.hidden = NO;
Heli.hidden = NO;
Heli.center = CGPointMake(88, 286);
Heli.image = [UIImage imageNamed:#"buss til app opp.png"];
Start = YES;
Scorenumber = 0;
Score.text = [NSString stringWithFormat:#"Score: 0"];
Intro3.text = [NSString stringWithFormat:#"HighScore: %i", HighScore];
}
} else{
and see - "}" symbol on line before "} else{" is wrong. It is the pair for
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
opening construction, so "else" is "standalone - it is wrong.
Try to delete this "}".
In my app I've having trouble to hear a sound. In particular this is my code:
#import <QuartzCore/QuartzCore.h>
#import <AudioToolbox/AudioToolbox.h>
#import "ViewController.h"
#import "RIOInterface.h"
#import "KeyHelper.h"
#import "Toast+UIView.h"
#interface ViewController () {
BOOL watermarkReceived;
float frequencyRecived;
CABasicAnimation *theAnimation;
BOOL water1, water2, water3, water4, noWater;
}
#property(nonatomic)NSTimer *timer, *timer2;
#property(nonatomic,strong)AVAudioPlayer *player;
#property(nonatomic,strong)NSURL *url;
#end
#implementation ViewController
#synthesize isListening;
#synthesize rioRef;
#synthesize currentFrequency;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.labelPosition.font=[UIFont fontWithName:#"DBLCDTempBlack" size:20.0];
NSError *error;
self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"sms_alert_circles" ofType:#"mp3"]];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.url error:&error];
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&setCategoryError];
[self.imageLed setImage:[UIImage imageNamed:#"image_led_red.png"]];
self.rioRef = [RIOInterface sharedInstance];
[rioRef setSampleRate:44100];
[rioRef setFrequency:394];//294
[rioRef initializeAudioSession];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)startListenWatermark:(UIButton *)sender {
if ([sender isSelected]) {
[self stopListener];
[UIApplication sharedApplication].idleTimerDisabled = NO;
[sender setSelected:NO];
[self.imageListening.layer removeAllAnimations];
[self.imageLed setImage:[UIImage imageNamed:#"image_led_red.png"]];
//self.labelPosition.font=[UIFont fontWithName:#"DBLCDTempBlack" size:20.0];
self.labelPosition.text = #"Nessuna postazione";
} else {
water1 = water2 = water3 = water4 = NO;
[self startListener];
[UIApplication sharedApplication].idleTimerDisabled = YES;
[sender setSelected:YES];
[self.imageLed setImage:[UIImage imageNamed:#"image_led_red.png"]];
self.labelPosition.text = #"Nessuna postazione";
theAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
theAnimation.duration = 0.4;
theAnimation.repeatDuration = 10000;
theAnimation.autoreverses = YES;
theAnimation.delegate = self;
theAnimation.fromValue = [NSNumber numberWithFloat:1.0];
theAnimation.toValue = [NSNumber numberWithFloat:0.1];
[self.imageListening.layer addAnimation:theAnimation forKey:#"animateOpacity"];
}
}
#pragma mark Listener methods
- (void)startListener {
[self.rioRef startListening:self];
}
- (void)stopListener {
[self.rioRef stopListening];
}
- (void)frequencyChangedWithValue:(float)newFrequency {
frequencyRecived = newFrequency;
watermarkReceived = YES;
if (frequencyRecived > 18000) {
if (frequencyRecived >= 18000 && frequencyRecived <= 18110 && !water1) {
[self.timer invalidate];
self.timer = nil;
[self performSelectorOnMainThread:#selector(setTextInLabel:) withObject:#"1" waitUntilDone:YES];
water2 = water3 = water4 = NO;
water1 = YES;
noWater = YES;
}
if (frequencyRecived >= 18115 && frequencyRecived <= 18250 && !water2) {
[self.timer invalidate];
self.timer = nil;
[self performSelectorOnMainThread:#selector(setTextInLabel:) withObject:#"2" waitUntilDone:YES];
water1 = water3 = water4 = NO;
water2 = YES;
noWater = YES;
}
if (frequencyRecived >= 18255 && frequencyRecived <= 18440 && !water3) {
[self.timer invalidate];
self.timer = nil;
[self performSelectorOnMainThread:#selector(setTextInLabel:) withObject:#"3" waitUntilDone:YES];
water1 = water2 = water4 = NO;
water3 = YES;
noWater = YES;
}
if (frequencyRecived >= 18445 && !water4) {
[self.timer invalidate];
self.timer = nil;
[self performSelectorOnMainThread:#selector(setTextInLabel:) withObject:#"4" waitUntilDone:YES];
water1 = water2 = water3 = NO;
water4 = YES;
noWater = YES;
}
} else {
if (noWater) {
[self performSelectorOnMainThread:#selector(noWatermark) withObject:nil waitUntilDone:YES];
noWater = NO;
}
}
}
- (void)noWatermark {
self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(noPosition:) userInfo:nil repeats:NO];
}
- (void)noPosition:(NSTimer*)aTimer {
[self performSelectorOnMainThread:#selector(setTextInLabel:) withObject:#"Nessuna postazione" waitUntilDone:YES];
[self performSelectorInBackground:#selector(redLed) withObject:nil];
self.timer2 = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(resetFlags) userInfo:nil repeats:NO];
}
- (void)resetFlags {
water1 = water2 = water3 = water4 = NO;
}
- (void)redLed {
[self.imageLed setImage:[UIImage imageNamed:#"image_led_red.png"]];
}
- (void)setTextInLabel:(NSString*)position {
[self.timer invalidate];
self.timer = nil;
if ([position isEqualToString:#"Nessuna postazione"]) {
self.labelPosition.text = position;
}
self.labelPosition.text = position;
if (![position isEqualToString:#"Nessuna postazione"]) {
[self.player setVolume:1.0];
[self.player prepareToPlay];
[self.player play];
NSString *textForToast = [NSString stringWithFormat:#"Postazione %#", position];
UIImage *image = [[UIImage alloc]init];
if ([position isEqualToString:#"1"]) {
image = [UIImage imageNamed:#"image_smart.png"];
}
if ([position isEqualToString:#"2"]) {
image = [UIImage imageNamed:#"image_500.png"];
}
if ([position isEqualToString:#"3"]) {
image = [UIImage imageNamed:#"image_mini.png"];
}
if ([position isEqualToString:#"4"]) {
image = [UIImage imageNamed:#"image_aygo.png"];
}
[self.view makeToast:textForToast duration:5.0 position:#"bottom" title:#"Watermark ricevuto" image:image];
[self.imageLed setImage:[UIImage imageNamed:#"image_led_green.png"]];
}
}
#end
In particular, this class should hear (with microphone) an audio signal in which there are some tone with a frequency >= 18000 Hz. So I will make this: when it recognize a tone of a frequency >= 18000 Hz it should play a sound.
When I try to run the app on the device I hear the sound with a very low volume by using iPhone speaker, but when I plug the headphone I hear the sound with an high volume. I tried to run the app by using simulator and when I use simulator it works nice. Why's that? Can you help me to fix this class?
PS: to detect the frequency of sounds I'm using pitch detector.
I solved it, I post here the code:
#import <QuartzCore/QuartzCore.h>
#import <AudioToolbox/AudioToolbox.h>
#import "ViewController.h"
#import "RIOInterface.h"
#import "KeyHelper.h"
#import "Toast+UIView.h"
#interface ViewController () {
BOOL watermarkReceived;
float frequencyRecived;
CABasicAnimation *theAnimation;
BOOL water1, water2, water3, water4, noWater;
}
#property(nonatomic)NSTimer *timer, *timer2;
//#property(strong)AVAudioPlayer *player;
#property(nonatomic,strong)NSURL *url;
#end
#implementation ViewController
#synthesize isListening;
#synthesize rioRef;
#synthesize currentFrequency;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// NSError *error;
//
// self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"sms_alert_circles" ofType:#"mp3"]];
// player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.url error:&error];
//
// AVAudioSession *session = [AVAudioSession sharedInstance];
//
// NSError *setCategoryError = nil;
// [session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&setCategoryError];
[self.imageLed setImage:[UIImage imageNamed:#"image_led_red.png"]];
self.rioRef = [RIOInterface sharedInstance];
[rioRef setSampleRate:44100];
[rioRef setFrequency:394];//294
[rioRef initializeAudioSession];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)startListenWatermark:(UIButton *)sender {
if ([sender isSelected]) {
[self stopListener];
[UIApplication sharedApplication].idleTimerDisabled = NO;
[sender setSelected:NO];
[self.imageListening.layer removeAllAnimations];
[self.imageLed setImage:[UIImage imageNamed:#"image_led_red.png"]];
//self.labelPosition.font=[UIFont fontWithName:#"DBLCDTempBlack" size:20.0];
self.labelPosition.text = #"Nessuna postazione";
} else {
water1 = water2 = water3 = water4 = NO;
[self startListener];
[UIApplication sharedApplication].idleTimerDisabled = YES;
[sender setSelected:YES];
[self.imageLed setImage:[UIImage imageNamed:#"image_led_red.png"]];
self.labelPosition.text = #"Nessuna postazione";
theAnimation = [CABasicAnimation animationWithKeyPath:#"opacity"];
theAnimation.duration = 0.4;
theAnimation.repeatDuration = 10000;
theAnimation.autoreverses = YES;
theAnimation.delegate = self;
theAnimation.fromValue = [NSNumber numberWithFloat:1.0];
theAnimation.toValue = [NSNumber numberWithFloat:0.1];
[self.imageListening.layer addAnimation:theAnimation forKey:#"animateOpacity"];
}
}
#pragma mark Listener methods
- (void)startListener {
[self.rioRef startListening:self];
}
- (void)stopListener {
[self.rioRef stopListening];
}
- (void)frequencyChangedWithValue:(float)newFrequency {
frequencyRecived = newFrequency;
watermarkReceived = YES;
if (frequencyRecived > 18000) {
if (frequencyRecived >= 18000 && frequencyRecived <= 18110 && !water1) {
[self.timer invalidate];
self.timer = nil;
[self performSelectorOnMainThread:#selector(setTextInLabel:) withObject:#"1" waitUntilDone:YES];
water2 = water3 = water4 = NO;
water1 = YES;
noWater = YES;
}
if (frequencyRecived >= 18115 && frequencyRecived <= 18250 && !water2) {
[self.timer invalidate];
self.timer = nil;
[self performSelectorOnMainThread:#selector(setTextInLabel:) withObject:#"2" waitUntilDone:YES];
water1 = water3 = water4 = NO;
water2 = YES;
noWater = YES;
}
if (frequencyRecived >= 18255 && frequencyRecived <= 18440 && !water3) {
[self.timer invalidate];
self.timer = nil;
[self performSelectorOnMainThread:#selector(setTextInLabel:) withObject:#"3" waitUntilDone:YES];
water1 = water2 = water4 = NO;
water3 = YES;
noWater = YES;
}
if (frequencyRecived >= 18445 && !water4) {
[self.timer invalidate];
self.timer = nil;
[self performSelectorOnMainThread:#selector(setTextInLabel:) withObject:#"4" waitUntilDone:YES];
water1 = water2 = water3 = NO;
water4 = YES;
noWater = YES;
}
} else {
if (noWater) {
[self performSelectorOnMainThread:#selector(noWatermark) withObject:nil waitUntilDone:YES];
noWater = NO;
}
}
}
- (void)noWatermark {
self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(noPosition:) userInfo:nil repeats:NO];
}
- (void)noPosition:(NSTimer*)aTimer {
[self performSelectorOnMainThread:#selector(setTextInLabel:) withObject:#"Nessuna postazione" waitUntilDone:YES];
[self performSelectorInBackground:#selector(redLed) withObject:nil];
self.timer2 = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(resetFlags) userInfo:nil repeats:NO];
}
- (void)resetFlags {
water1 = water2 = water3 = water4 = NO;
}
- (void)redLed {
[self.imageLed setImage:[UIImage imageNamed:#"image_led_red.png"]];
}
- (void)setTextInLabel:(NSString*)position {
[self.timer invalidate];
self.timer = nil;
NSError *error;
self.url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"sms_alert_circles" ofType:#"mp3"]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:self.url error:&error];
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *setCategoryError = nil;
[session setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:&setCategoryError];
if ([position isEqualToString:#"Nessuna postazione"]) {
self.labelPosition.text = position;
}
self.labelPosition.text = position;
if (![position isEqualToString:#"Nessuna postazione"]) {
[player setVolume:1.0];
[player prepareToPlay];
[player play];
NSString *textForToast = [NSString stringWithFormat:#"Postazione %#", position];
UIImage *image = [[UIImage alloc]init];
if ([position isEqualToString:#"1"]) {
image = [UIImage imageNamed:#"image_smart.png"];
}
if ([position isEqualToString:#"2"]) {
image = [UIImage imageNamed:#"image_500.png"];
}
if ([position isEqualToString:#"3"]) {
image = [UIImage imageNamed:#"image_mini.png"];
}
if ([position isEqualToString:#"4"]) {
image = [UIImage imageNamed:#"image_aygo.png"];
}
[self.view makeToast:textForToast duration:5.0 position:#"bottom" title:#"Watermark ricevuto" image:image];
[self.imageLed setImage:[UIImage imageNamed:#"image_led_green.png"]];
}
}
#end
I just put the initializer of the AVAudioPlayer into method setTextInLabel and it works.
Thank you!
I am new to App Development, sorry if I am asking silly questions :(
I am developing a Quiz App. Whenever I give a right or wrong answer, an image appears with right or wrong answer result, however it stays for a few seconds, but after that - it does not continue with the next question. It just shows my background images with buttons. How can I set a code, that it goes straight to a new question, when the result image is gone.
I will be very thankful for your help.
I just want that after the Result-image closes, the quiz continues with the next question.
This is the code i have setup in Game.m file.
-(void)RightAnswer{
ScoreNumber = ScoreNumber + 1;
Score.text = [NSString stringWithFormat:#"%i", ScoreNumber];
Answer1.hidden = YES;
Answer2.hidden = YES;
Answer3.hidden = YES;
CategorySelected.hidden = NO;
Next.hidden = NO;
imageQuestion.hidden = YES;
Results.hidden = NO;
Results.image = [UIImage imageNamed:#"right.png"]; [self performSelector:#selector(Results) withObject:nil afterDelay:2.0];
GameInProgress = YES;
}
-(void)WrongAnswer{
LivesNumber = LivesNumber - 1;
Lives.text = [NSString stringWithFormat:#"%i", LivesNumber];
Answer1.hidden = YES;
Answer2.hidden = YES;
Answer3.hidden = YES;
imageQuestion.hidden = YES;
CategorySelected.hidden = NO;
Next.hidden = NO;
Results.hidden = NO;
Results.image = [UIImage imageNamed:#"wrong.png"];[self performSelector:#selector(Results) withObject:nil afterDelay:2.0];
GameInProgress = YES;
if(LivesNumber ==0) {
Results.image = [UIImage imageNamed:#"gameover.png"];
GameInProgress = NO;
Exit.hidden = NO;
}
-(void)RightAnswer{
ScoreNumber = ScoreNumber + 1;
Score.text = [NSString stringWithFormat:#"%i", ScoreNumber];
Answer1.hidden = YES;
Answer2.hidden = YES;
Answer3.hidden = YES;
Answer1Correct ==YES;
CategorySelected.hidden = NO;
Next.hidden = NO;
imageQuestion.hidden = YES;
Results.hidden = NO;
Results.image = [UIImage imageNamed:#"right.png"]; [self performSelector:#selector(Results) withObject:nil afterDelay:2.0];
GameInProgress = YES;
}
I think the issue is that you never set Answer1Correct to anything. so if you add Answer1Correct ==YES to the correct answer method and Answer1Correct ==NO to the wrong method then it should work
-(void)Results{ // just change formate of answer.
Results.hidden = YES;
}
-(IBAction)Answer1: (id) sender{
if (Answer1Correct ==YES) {
[self RightAnswer];
}else{
[self WrongAnswer];
}
}
-(IBAction)Answer2: (id) sender{
if (Answer2Correct ==YES) {
[self RightAnswer];
}
else{
[self WrongAnswer];
}
}
-(IBAction)Answer3: (id) sender{
if (Answer3Correct ==YES) {
[self RightAnswer];
}
else{
[self WrongAnswer];
}
}
i may be wrong here but I think you need an if to the else.
But I could be wrong.
if(LivesNumber ==0) {
Results.image = [UIImage imageNamed:#"gameover.png"];
GameInProgress = NO;
Exit.hidden = NO;
}else{
donextcard();
}
if you want to know how to make that function we would need to see more of the code.