Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-03-2011, 12:41 AM   #1 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default Shake phone and play sound

Clearly I'm missing out on somthing and i know its some stupid mistake in my code but my eyes are not catching it. I have a list of arraySounds that play fine with a button. But now i want it to play a random sound when you shake the phone.

Heres my code:

Code:
// SHAKE START --------------------------------------

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(event.type == UIEventSubtypeMotionShake)
    {
        NSLog(@"Iphone has been shook");
        {
{
int index = arc4random % [soundArray count];
AVAudioPlayer* theSound = [soundArray objectAtIndex: index];
[theSound play];
}
        }
    }
}

- (BOOL)canBecomeFirstResponder
{ 
    return YES; 
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}

// SHAKE END ---------------------------------

Thanks for any suggestions!

Last edited by aceiswild; 11-03-2011 at 12:48 AM. Reason: code change
aceiswild is offline   Reply With Quote
Old 11-03-2011, 01:45 AM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Your use of { } do not make sense at all. Why do I have a feeling you are copying and pasting code without really knowing how it works...

This is how you use braces correctly:
Code:
if (this)
{
     //run this code
    //and that
}

if (this)
{

    if (that)
    {
       //run some code
       //run some more code
    }

}
Excuse my terrible formatting, for some reason, I can't use tab here.

Here is your code partly fixed:
Code:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(event.type == UIEventSubtypeMotionShake)
    {
        NSLog(@"Iphone has been shook"); {
        int index = arc4random % [soundArray count];
        AVAudioPlayer* theSound = [soundArray objectAtIndex: index];
        [theSound play];
    }
}
There is one "{" that does not belong. Find it and remove it.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.

Last edited by Domele; 11-03-2011 at 01:48 AM.
Domele is offline   Reply With Quote
Old 11-03-2011, 04:12 PM   #3 (permalink)
Registered Member
 
aceiswild's Avatar
 
Join Date: Oct 2011
Location: Canada
Posts: 62
aceiswild is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
Your use of { } do not make sense at all. Why do I have a feeling you are copying and pasting code without really knowing how it works...

This is how you use braces correctly:
Code:
if (this)
{
     //run this code
    //and that
}

if (this)
{

    if (that)
    {
       //run some code
       //run some more code
    }

}
Excuse my terrible formatting, for some reason, I can't use tab here.

Here is your code partly fixed:
Code:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    if(event.type == UIEventSubtypeMotionShake)
    {
        NSLog(@"Iphone has been shook"); {
        int index = arc4random % [soundArray count];
        AVAudioPlayer* theSound = [soundArray objectAtIndex: index];
        [theSound play];
    }
}
There is one "{" that does not belong. Find it and remove it.

Thanks. Havnt done the changes yet. Not home. Actually I never copied and pasted that code. The reason why I had extra brackets it I forgot to take them out from my other sound code before I decided to randomize it. But I had no errors when I removes the sound play code and ran it. I'm still getting used to objective c coming from pure web design. I have to say you a mean but awesome teacher! Lol you should make a tutorial/business website.
aceiswild is offline   Reply With Quote
Reply

Bookmarks

Tags
play, shake, sound

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 415
17 members and 398 guests
Alex-alex, Apptronics RBC, baja_yu, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, matador1978, n00b, pbart, reficul, Retouchable, Sami Gh, usernametaken, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:18 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0