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

View Single Post
Old 01-07-2010, 07:18 PM   #1 (permalink)
iphonig
Registered Member
 
iphonig's Avatar
 
Join Date: Sep 2009
Location: United States
Posts: 73
iphonig is on a distinguished road
Default 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
__________________
Check Out All of Our Apps Here!
iphonig is offline   Reply With Quote
 

» Advertisements
» Online Users: 507
18 members and 489 guests
AH24, ArthurOff, baja_yu, bignoggins, dbramhall, djohnson, Falcon80, fanarin, Feldspar, habitatus, myach, Nobbsy, PavelSea, pbart, Sami Gh, skog, Thompson22, Warblr
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,427
Threads: 94,023
Posts: 402,575
Top Poster: BrianSlick (7,978)
Welcome to our newest member, shakiraeb60
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:02 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.