That code looks OK to me. You have an image named "STone.png" in the project, right? How does it now work - the stones don't appear, or they don't move?
Put these three logs at the end of createMeteors:
Code:
NSLog(@"meteorImage is: %@", meteorImage);
NSLog(@"meteorArray is: %@", meteorArray);
NSLog(@"self.view is: %@", self.view);
If any of them print null in the console, you have a problem. If meteorImage is null then you have the filename wrong.
If you post more code, put [code] [ /code] tags around it.
Ohh, Okay, This is my new code, but it dosent work
Code:
@synthesize myRock, meteorArray;
-(void)viewDidLoad {
[self createMeteors];
[NSTimer scheduledTimerWithTimeInterval:(0.1) target:self selector:@selector(moveMeteors) userInfo:nil repeats:YES];
[super viewDidLoad];
}
-(void)createMeteors{
if (meteorArray==nil){
meteorArray = [[NSMutableArray alloc] init];
}
UIImage *meteorImage = [UIImage imageNamed:@"STone.png"];
UIImageView *newView; // This should be declared in the .h file
for (int i = 0; i< 3; i++){ //creates meteors
newView= [[UIImageView alloc] initWithImage: meteorImage];
//pick a position *above* the top of the screen
int x = arc4random()%320;
int y = -arc4random()%480;
double scale = 1 / round(random() % 100) + 1.0;
double speed = 1 / round(random() % 100) + 1.0;
newView.center = CGPointMake (x,y);
[self.view addSubview: newView]; //adds meteors as subviews
[meteorArray addObject: newView]; //add to array
[newView release];
}
}
-(void)moveMeteors{
for (UIImageView* myRock in meteorArray ){
//move new center down
CGPoint newCenter = myRock.center;
newCenter.y = newCenter.y +10;
if (newCenter.y > 480){ // if off the bottom
newCenter.y=0; //pop to top
newCenter.x = arc4random()%320; //and pick new x position
}
myRock.center = newCenter;
}
}
I believe this line is the problem:
Code:
UIImageView *newView;
Because then you are trying to access it from a different method, and it's not declared globally.
Have a look at what I've bolded.
EDIT: Also is moveMeteors being called from an NSTimer? Just curious.
ZunePod: newView doesn't need to be in the .h file, because it's just a temp variable we're using to create the new views. The array meteorArray should be in the .h file, though.
ZunePod: newView doesn't need to be in the .h file, because it's just a temp variable we're using to create the new views. The array meteorArray should be in the .h file, though.
I dont know what is wrong, the console dosent say Collision, when the Fallingimage hit the Elephant?
I dont konw what is wrong, the console dosn't say Collision, when the Falling image hit the Elephant?
Are you using objectsArray, or meteorArray like you had in the other code? I would log the contents of the array at the start of this method to make sure that (1) the method is really getting called and (2) objectsArray has some items in it.
I am a newbie programmer and i saw your coding and tried in my example but its showing touchPosition undeclared error is showing. Could you tell me why this error is showing in the example.
Could you send the source code how you reacting CGRectContainsPoint(aView.frame,touchPosition) == YES) when i add this in my coding it shows CGRectContainsPoint does not have reference and symbols not found...
within my .m file, in "viewDidLoad", I have the following code:
Code:
if (fruitArray == nil) { [[NSMutableArray alloc] init];}
UIImage *fruitImage = [UIImage imageNamed:@"banana.png"];
UIImageView *newView;
for (int i=0; i < current_level; i++)
{
newView = [[UIImageView alloc] initWithImage:fruitImage];
int x = arc4random()%320;
int y = (arc4random()%480);
newView.center = CGPointMake(x,y);
[self.view addSubview:newView];
[fruitArray addObject:newView];
[newView release];
}
then within my game loop I have:
Code:
// Code for falling fruit - NOT WORKING
for (UIImageView* tempView in fruitArray )
{
NSLog(@"yay");
//move new center down
CGPoint newCenter = tempView.center;
newCenter.y = newCenter.y +10;
if (newCenter.y > 480){ // if off the bottom
newCenter.y=0; //pop to top
newCenter.x = arc4random()%320; //and pick new x position
}
tempView.center = newCenter;
}
Since I never see "yay" in the console, I assume that my array is empty. Any ideas why this is happening?
Would you know how to put an individual score on a "rock" so it takes e.g:5 taps on one 'rock" to remove the rock view?
Thanks
At that point you can no longer have an array of UIViews - you should probably make your own "Rock" class that inherits from NSObject and has a pointer to a UIImageView, along with a counter for the score or hit points or velocity or angle or whatever else you need.
At that point you can no longer have an array of UIViews - you should probably make your own "Rock" class that inherits from NSObject and has a pointer to a UIImageView, along with a counter for the score or hit points or velocity or angle or whatever else you need.
Could you give me a few pointers on how I would that up, and use it on a large number scale and randomness that I could use with the Random Rock example?
Could you give me a few pointers on how I would that up, and use it on a large number scale and randomness that I could use with the Random Rock example?
For info on creating your own class, see the sections on "Designing a Class Interface" and "Class Implementation" on this page: Cocoa Dev Central: Learn Objective-C
Whatever book you're using to learn objective-C must have a chapter on that too.
Once you have your own Rock class with a "view" property and whatever variables and properties you need, you can init that class:
For info on creating your own class, see the sections on "Designing a Class Interface" and "Class Implementation" on this page: Cocoa Dev Central: Learn Objective-C
Whatever book you're using to learn objective-C must have a chapter on that too.
Once you have your own Rock class with a "view" property and whatever variables and properties you need, you can init that class:
For info on creating your own class, see the sections on "Designing a Class Interface" and "Class Implementation" on this page: Cocoa Dev Central: Learn Objective-C
Whatever book you're using to learn objective-C must have a chapter on that too.
Once you have your own Rock class with a "view" property and whatever variables and properties you need, you can init that class:
I find that this implementation makes the game quite choppy.
Using UIViews you're not going to get more than 15 or 20 FPS. I wasn't able to get a reliable 30FPS.
I still think this is a good way to make a "first game" and learn about Objective-C and UIViews. It's not a replacement for OpenGL or Cocos2D, though.
Quote:
Originally Posted by Batman
i feel completely lost on this whole class thing. i read those parts of the website but it did not say how to access methods in that class.
The same way you access methods on any class - if you wrote a "move" method for your Rock class, you'd say [myRock move] . Creating classes is in no way specific to this little tutorial - any book or web site on Objective-C will teach you how to create a class and call methods.
Hello, Download my new game FruityQuiz which teaches you names of fruits in 5 different languages. Its free for limited time. Enjoy and leave a good feedback.
put this in the shameless advertising, don't just randomly post it in a thread.
@SMASHER OR ANY1ELSE "Ok soo i did this code and i have those images falling but there's a bug!I have a label that turns to 1 from 0 when that falling image collides with an image below BUT there is a bug when i starts falling down it just automatically adds 1 to my label where as i want tht 1 to add only when it collides!!Please help"
Here is the code