Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.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 01-07-2010, 08:18 PM   #1 (permalink)
Registered Member
 
iphonig's Avatar
 
Join Date: Sep 2009
Location: United States
Posts: 73
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
Old 01-07-2010, 09:55 PM   #2 (permalink)
ftm
FasterThanMonkeys.com
 
Join Date: Mar 2009
Location: Southern California
Posts: 523
Send a message via AIM to ftm
Default

You have defined a loadBackground method, but I don't see that you are calling it anywhere. Are you calling it externally?
__________________


Website: http://fasterthanmonkeys.com

iScore Baseball Scorekeeper Top baseball scorekeeping app for iPhone
Bug Squash Reached #1 kids game in 15+ countries including USA, now with OpenFeint
Jam Packed! Fun puzzle game for all ages
Jam Packed Christmas Holiday puzzle game!
iScore Basketball Scorekeeper Best basketball scorekeeping application
ftm is offline   Reply With Quote
Old 01-07-2010, 10:06 PM   #3 (permalink)
Registered Member
 
iphonig's Avatar
 
Join Date: Sep 2009
Location: United States
Posts: 73
Default

Quote:
Originally Posted by ftm View Post
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
__________________
Check Out All of Our Apps Here!
iphonig is offline   Reply With Quote
Old 01-07-2010, 10:10 PM   #4 (permalink)
ftm
FasterThanMonkeys.com
 
Join Date: Mar 2009
Location: Southern California
Posts: 523
Send a message via AIM to ftm
Default

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.
__________________


Website: http://fasterthanmonkeys.com

iScore Baseball Scorekeeper Top baseball scorekeeping app for iPhone
Bug Squash Reached #1 kids game in 15+ countries including USA, now with OpenFeint
Jam Packed! Fun puzzle game for all ages
Jam Packed Christmas Holiday puzzle game!
iScore Basketball Scorekeeper Best basketball scorekeeping application
ftm is offline   Reply With Quote
Reply

Bookmarks

Tags
accelerometer, image, labyrinth, load

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: 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
Powered by vBadvanced CMPS v3.1.0

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