I'm having trouble getting the value of a property of my class. My class is called ball and I'd like to get it's velocity (which i set but can change) from my view control.
Here is my ball.h code:
Here is my part of my ball.m code:
Here is my viewVontroller.h
and here is the relative code from my viewController.m
When I get to the following line in debug mode the game in my iPhone Simulator freezes and I do not see any errors in xCode (probably don't know where to look):
I think I'm just not setting up my ball class correctly to be able to pull back the value of the ballVelocity variable. Can anyone point me in the correct direction?
Here is my ball.h code:
Code:
#import <Foundation/Foundation.h>
@interface Ball : UIImageView {
CGPoint ballVelocity;
int mXSpeed;
int mYSpeed;
float mAngle;
}
- (void)getVelocity:(CGPoint)speed;
- (void)move;
- (void)bounceHorizontal;
- (void)bounceVertical;
- (void)setSpeedX:(int)xSpeed Y:(int)ySpeed;
- (void)setVelocity:(CGPoint)speed;
- (CGPoint) ballVelocity;
@end
Here is my part of my ball.m code:
Code:
#import "Ball.h"
@implementation Ball
- (void)setVelocity:(CGPoint)speed {
ballVelocity = speed;
}
Here is my viewVontroller.h
Code:
#import <UIKit/UIKit.h>
@class Ball;
@interface BallsOfFireViewController : UIViewController {
IBOutlet UIImageView *ball;
NSMutableArray *mBallArray;
CGPoint ballVelocity;
CGPoint velocityz;
CGPoint velocityz2;
}
@property(nonatomic, retain) IBOutlet UIImageView *ball;
@property(retain, nonatomic) IBOutlet NSMutableArray *mBallArray;
@property(nonatomic) CGPoint ballVelocity;
- (void) checkCollision;
@end
and here is the relative code from my viewController.m
Code:
#import "BallsOfFireViewController.h"
#import "Ball.h"
//#define kGameStateRunning 1
//#define kGameStatePaused 2
#define kBallSpeedX 1
#define kBallSpeedY 1
@implementation BallsOfFireViewController
@synthesize ball, mBallArray, ballVelocity;
- (void)viewDidLoad {
[super viewDidLoad];
//self.gameState = kGameStatePaused;
mBallArray = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < 3; i++)
{
Ball* ballx=[[Ball alloc] initWithImage:[UIImage imageNamed:@"FireBall.png"]];
[mBallArray addObject:ballx];
[self.view addSubview:ballx];
if (i == 14
){
//[ballx setBackgroundColor:([UIColor redColor])];
ballx.image = [UIImage imageNamed:@"IceBall.png"];
}
ballVelocity = CGPointMake(1,1);
[ballx setVelocity:ballVelocity];
ballx.center = CGPointMake((random() % 14) * 20 + 20 ,(random() % 20) * 20 + 20);
}
[NSTimer scheduledTimerWithTimeInterval:0.005
target:self
selector:@selector(moveBall)
userInfo:nil
repeats:YES];
}
-(void)moveBall
{
int i;
int numberOfBalls=[mBallArray count];
for (i = 0; i < numberOfBalls; i++)
{
Ball* ballz=[mBallArray objectAtIndex:i];
int i2;
for (i2 = 0; i2 < numberOfBalls; i2++)
if (i2 != i) {
Ball* ballz2=[mBallArray objectAtIndex:i2];
if (CGRectIntersectsRect(ballz.frame, ballz2.frame)) {
if (sqrt(pow(ballz.center.x - ballz2.center.x,2) + pow(ballz.center.y - ballz2.center.y,2)) <= pow(ballz.frame.size.width,2)) {
[ballz move];
velocityz = ballz.ballVelocity;
velocityz2 = ballz2.ballVelocity;
if (velocityz.x == velocityz2.x * -1 && velocityz.y == velocityz2.y * -1) {
if (pow(ballz.center.x - ballz2.center.x,2) > pow(ballz.center.y - ballz2.center.y,2)) {
[ballz bounceHorizontal];
[ballz2 bounceHorizontal];
} else {
[ballz bounceVertical];
[ballz2 bounceVertical];
}
}
}
}
}
[ballz move];
}
}
When I get to the following line in debug mode the game in my iPhone Simulator freezes and I do not see any errors in xCode (probably don't know where to look):
Code:
velocityz = ballz.ballVelocity;
I think I'm just not setting up my ball class correctly to be able to pull back the value of the ballVelocity variable. Can anyone point me in the correct direction?