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 03-03-2011, 06:10 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 35
Kenenth is on a distinguished road
Default Help on Objective C NSMutableArrays and For Loops?

I am 12 and trying to make an iphone app. In a part of the app, paratroopers must scroll down the screen, but I have to keep track of them. I have done so with an NSMutableArray. I create a random image at 1 y and a random value x and I store it in my array. I go through all of the items with a for loop and I am trying to lower the y value so that they come down the screen. this is my code for it:

-(void)lowerParatroopers {

for (int x=1; x<=[array count]; x++) {
((UIImageView *) [array objectAtIndex:x ]).center.y = ((UIImageView *)[array objectAtIndex:x ]).center.y-1;
}
}


Every time I change it to work another error pops up. Can you please share the code to be able to do this function? Or if i am close to getting it right, can you please tell me what I am doing wrong? Thanks!
Kenenth is offline   Reply With Quote
Old 03-03-2011, 07:15 AM   #2 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

1)Don't come in here and start announcing your age, that will tick me off
2)Start x at 0
3)
Code:
[[array objectAtIndex:x] setCenter:CGPointMake([[[array objectAtIndex:x] center] x], [[[array objectAtIndex:x] center] y]-1)];
__________________
iisword is offline   Reply With Quote
Old 03-03-2011, 07:44 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2009
Location: Amsterdam, The Netherlands
Posts: 782
TUX2K is on a distinguished road
Default

Like iisword already said array start at 0.

also make sure that the x is always smaller the the count:
Code:
for (int x=0; x < [array count]; x++) {
Else you will get an index out of bounds error.
__________________
If my answer helped you, you might want to help me.
Make a donation via PayPal.
TUX2K is offline   Reply With Quote
Old 03-03-2011, 09:48 AM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

You got two right answers, but I'm going to put them together and make them easier to read.

Code:
for (int x=0; x < [array count]; x++) {
     UIImageView *myView = [array objectAtIndex:x];
     CGPoint newCenter = CGPointMake(myView.center.x, myView.center.y-1);
     myView.center = newCenter;
}
You could also go one easier as long as you don't intend to remove objects from the array:
Code:
for (UIImageView *myView in array) {
     CGPoint newCenter = CGPointMake(myView.center.x, myView.center.y-1);
     myView.center = newCenter;
}
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Tags
arrays, for loops, iphone, nsmutablearrays, sdk

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: 368
16 members and 352 guests
Absentia, akphyo, apatsufas, BinHex, dre, Error404, Gaz, gmarro, jeroenkeij, Kirkout, MarkC, mottdog, Music Man, PavelMik, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,666
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, cpsclicker
Powered by vBadvanced CMPS v3.1.0

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