01-07-2010, 08:18 PM
#1 (permalink )
Registered Member
Join Date: Sep 2009
Location: United States
Posts: 73
Draw image problem
Hey all,
I was making a test Labyrinth application just for fun
and came accross a problem. I am able to have a marble move about the screen by tilting the iphone with the accelerometer however I am not able to get the level to load. I made the level on photoshop so it is an image. I tried to create the image as being the background for the view but when I do that I get an error. I have 4 classes a LabyrinthTestViewController.h/.m and a LabyrinthBall.h/.m the LabyrinthTestViewController.xib's view loads from the LabyrinthBall class.
Here is the .h for the LabyrinthBall.h
Code:
#define kVelocityMultiplier 500
#import <UIKit/UIKit.h>
@interface BallView : UIView {
UIImage *image;
CGPoint currentPoint;
CGPoint previousPoint;
UIAcceleration *acceleration;
CGFloat ballXVelocity;
CGFloat ballYVelocity;
}
@property (nonatomic, retain)UIImage *image;
@property CGPoint currentPoint;
@property CGPoint previousPoint;
@property (nonatomic, retain) UIAcceleration *acceleration;
@property CGFloat ballXVelocity;
@property CGFloat ballYVelocity;
- (void)draw;
@end
and here is the .m
Code:
@synthesize image;
@dynamic currentPoint;
@synthesize previousPoint;
@synthesize acceleration;
@synthesize ballXVelocity;
@synthesize ballYVelocity;
- (id)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
self.image = [UIImage imageNamed:@"ball.png"];
self.currentPoint = CGPointMake((self.bounds.size.width / 2) + (image.size.width / 2), (self.bounds.size.height / 2) + (image.size.height / 2));
ballXVelocity = 0.0f;
ballYVelocity = 0.0f;
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
}
return self;
}
-(void)loadBackground{
CGRect mainBackgroundFrame = CGRectMake(0, 0, 320, 480);
UIImageView *mainBackground = [[UIImageView alloc] initWithFrame:mainBackgroundFrame];
mainBackground.image = [UIImage imageNamed:@"Labyrinth.png"];
[self.view addSubview:mainBackground];
[mainBackground release];
}
- (void)drawRect:(CGRect)rect {
[image drawAtPoint:currentPoint];
}
- (CGPoint)currentPoint {
return currentPoint;
}
- (void)draw {
static NSDate *lastDrawTime;
if (lastDrawTime != nil) {
NSTimeInterval secondsSinceLastDraw = -([lastDrawTime timeIntervalSinceNow]);
ballYVelocity = ballYVelocity + -(acceleration.y * secondsSinceLastDraw);
ballXVelocity = ballXVelocity + acceleration.x * secondsSinceLastDraw;
CGFloat xAcceleration = secondsSinceLastDraw * ballXVelocity * kVelocityMultiplier;
CGFloat yAcceleration = secondsSinceLastDraw * ballYVelocity * kVelocityMultiplier;
self.currentPoint = CGPointMake(self.currentPoint.x + xAcceleration, self.currentPoint.y +yAcceleration);
}
// Update last time with current time
[lastDrawTime release];
lastDrawTime = [[NSDate alloc] init];
}
- (void)setCurrentPoint:(CGPoint)newPoint {
previousPoint = currentPoint;
currentPoint = newPoint;
if (currentPoint.x < 0) {
currentPoint.x = 0;
ballXVelocity = 0;
}
if (currentPoint.y < 0){
currentPoint.y = 0;
ballYVelocity = 0;
}
if (currentPoint.x > self.bounds.size.width - image.size.width) {
currentPoint.x = self.bounds.size.width - image.size.width;
ballXVelocity = 0;
}
if (currentPoint.y > self.bounds.size.height - image.size.height) {
currentPoint.y = self.bounds.size.height - image.size.height;
ballYVelocity = 0;
}
CGRect currentImageRect = CGRectMake(currentPoint.x, currentPoint.y, currentPoint.x + image.size.width, currentPoint.y + image.size.height);
CGRect previousImageRect = CGRectMake(previousPoint.x, previousPoint.y, previousPoint.x + image.size.width, currentPoint.y + image.size.height);
[self setNeedsDisplayInRect:CGRectUnion(currentImageRect, previousImageRect)];
}
- (void)dealloc {
[image release];
[acceleration release];
[super dealloc];
}
@end
Any suggestions would be appreciated
01-07-2010, 09:55 PM
#2 (permalink )
FasterThanMonkeys.com
Join Date: Mar 2009
Location: Southern California
Posts: 523
You have defined a loadBackground method, but I don't see that you are calling it anywhere. Are you calling it externally?
01-07-2010, 10:06 PM
#3 (permalink )
Registered Member
Join Date: Sep 2009
Location: United States
Posts: 73
Quote:
Originally Posted by
ftm
You have defined a loadBackground method, but I don't see that you are calling it anywhere. Are you calling it externally?
i get an error in that loadBackground saying view is not a struct or union. I would put [self loadBackground] in the initWithCoder method? correct
01-07-2010, 10:10 PM
#4 (permalink )
FasterThanMonkeys.com
Join Date: Mar 2009
Location: Southern California
Posts: 523
If it is something that will never change, I would think you would want it in viewDidAppear or viewWillAppear. If it is going to be changing (like user chooses a different level, etc...) then I would think you would have an external class making the call and passing in the level to load.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 246
22 members and 224 guests
ADY , Alsahir , beleg_1998 , Dani77 , diyora , FAED , fredidf , iDifferent , iph_s , JamesCahall , JasonR , mer10 , prchn4christ , Rudy , smithdale87 , Speed , spiderguy84 , stekki , tgjorgoski , timle8n1 , twerner , vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris