Well to start off, THANK YOU, all of you. And to be honest the college class I am in, I kinda jumped into before I was ready. I should of taken some more programming courses (I just have Fundamentals of C++ and C++ One under my belt), so now instead of doing text programs (like I probly should be working on) I am trying to build a game. And right now its either, build the game or fail the course, and I really don't want to fail and I figure I'll learn a lot under pressure. Heres my code:
GameViewController.h
GameViewController.m
The next set is where my "touches moved" and my attempt at collision is. The touches moved is based on a lesson found in iPhone and iPad Apps for Absolute Beginners by Dr. Rory Lewis
TransformView.h
TransformView.m
Heres what I do know. I know that doing the .center.x and .center.y is a bad choice since that point alone is the size of a pixel so the player would have to be ridiculously accurate for it to work. I know that I should use an "area" of some sort (a rectangle most likely) to write a condition if the object is within a certain area, then win.
Heres what I don't know. I don't know if my win condition even works, it doesn't give me an error but I haven't seen it execute. It hasn't executed either due to it not working, or do to my condition statement to be wrong. One or the other is written improperly maybe both.
A little bit about the kind of game I am trying to accomplish. Its a little kids game, where I want to do basic things, like fitting objects in the right spots like a puzzle piece. Which is why I need to figure out a coordinate event of some sort. And the reason why I also included rotating and scaling is because later in the game for a "harder" difficulty I want the player to stretch or shrink the image to a certain size or rotate it to a certain degree to win. Its a very basic game I and my fellow students are trying to execute. Our coding team is myself and 2 other kids, and were all about as good as each other. The books I have and use are, Programming in Objective C 2.0 by Stephen Kochan and Learning iPhone Game Programming by Michael Daley. I was borrowing the Rory Lewis book from a friend. Thanks for you're help, I am looking through the books trying to come up with something. Their isn't a tutor at school for iPhone development but a friend told me about here. Between advice here, the books, self determination, I am sure I will get it done, I won't give up
GameViewController.h
Code:
// Created by Brandon on 10/11/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TransformView.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface GameViewController : UIViewController {
IBOutlet UIImageView *objCircleBlue;
IBOutlet UIImageView *objCircleBlueHole;
IBOutlet UIImageView *objCircleRed;
}
@property (nonatomic, retain) IBOutlet UIImageView *objCircleBlue;
@property (nonatomic, retain) IBOutlet UIImageView *objCircleBlueHole;
@property (nonatomic, retain) IBOutlet UIImageView *objCircleRed;
@end
GameViewController.m
Code:
// Created by Brandon on 10/11/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "GameViewController.h"
@implementation GameViewController
@synthesize objCircleBlue, objCircleBlueHole, objCircleRed;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName: (NSString *)nibNameOrNil bundle: (NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"SpaceBrandon.mp3"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"" ofType:@"mp3"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
//UIAlertView *alertBreak = [[UIAlertView alloc] initWithTitle:@"SoundPath" message: path delegate: nil cancelButtonTitle: @"OK" otherButtonTitles:nil]; [alertBreak show]; [alertBreak release];
AVAudioPlayer *audioPlayer;
//NSString *Path = [[NSBundle mainBundle] pathForResource:@"SpaceBrandon" ofType@"mp3"];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
//audioPlayer.numberOfLoops = -1;
[audioPlayer play];
//TransformView* theTouchView = [[TransformView alloc] initWithImage:[UIImage imageNamed:@"Circle6.png"]];
//[theTouchView setFrame:CGRectMake(110, 180, [theTouchView frame].size.width, [theTouchView frame].size.height)];
//[[self view] addSubview:theTouchView];
//[theTouchView release];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc
{
[super dealloc];
}
@end
The next set is where my "touches moved" and my attempt at collision is. The touches moved is based on a lesson found in iPhone and iPad Apps for Absolute Beginners by Dr. Rory Lewis
TransformView.h
Code:
// Created by Brandon on 11/14/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface TransformView : UIImageView
{
UITouch* firstTouch;
UITouch* secondTouch;
IBOutlet UIImageView *objCircleBlue;
IBOutlet UIImageView *objCircleBlueHole;
IBOutlet UIImageView *objCircleRed;
}
@property (nonatomic, retain) IBOutlet UIImageView *objCircleBlue;
@property (nonatomic, retain) IBOutlet UIImageView *objCircleBlueHole;
@property (nonatomic, retain) IBOutlet UIImageView *objCircleRed;
- (float) angleBetweenThisPoint:(CGPoint)firstPoint andThisPoint:(CGPoint)secondPoint;
- (float) distanceBetweenThisPoint:(CGPoint)firstPoint andThisPoint:(CGPoint)secondPoint;
@end
TransformView.m
Code:
// Created by Brandon on 11/14/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "TransformView.h"
@implementation TransformView
@synthesize objCircleBlue, objCircleBlueHole, objCircleRed;
- (id) initWithImage:(UIImage *)image
{
if (self = [super initWithImage:image])
{
[self setUserInteractionEnabled:YES];
[self setMultipleTouchEnabled:YES];
}
return self;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//Single touch
if ([touches count] == 1)
{
if (!firstTouch)
{
firstTouch = [[touches anyObject] retain];
}
else if (!secondTouch)
{
secondTouch = [[touches anyObject] retain];
}
}
//Multiple touch
if ([touches count] == 2)
{
NSArray* theTouches = [touches allObjects];
[firstTouch release];
[secondTouch release];
firstTouch = nil;
secondTouch = nil;
firstTouch = [[theTouches objectAtIndex:0] retain];
secondTouch = [[theTouches objectAtIndex:1] retain];
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([touches count] == 1)
{
CGPoint newTouch = [[touches anyObject] locationInView:[self superview]];
CGPoint lastTouch = [[touches anyObject] previousLocationInView:[self superview]];
float xDif = newTouch.x - lastTouch.x;
float yDif = newTouch.y - lastTouch.y;
CGAffineTransform translate = CGAffineTransformMakeTranslation(xDif, yDif);
[self setTransform: CGAffineTransformConcat([self transform], translate)];
}
else if ([touches count] == 2 && firstTouch && secondTouch)
{
//Rotate
float newAngle = [self angleBetweenThisPoint:[firstTouch locationInView:[self superview]]
andThisPoint:[secondTouch locationInView:[self superview]]];
float oldAngle = [self angleBetweenThisPoint:[firstTouch previousLocationInView:[self superview]]
andThisPoint:[secondTouch previousLocationInView:[self superview]]];
CGAffineTransform rotation = CGAffineTransformMakeRotation(oldAngle - newAngle);
[self setTransform: CGAffineTransformConcat([self transform], rotation)];
//Scale
float newDistance = [self distanceBetweenThisPoint:[firstTouch locationInView:[self superview]]
andThisPoint:[secondTouch locationInView:[self superview]]];
float oldDistance = [self distanceBetweenThisPoint:[firstTouch previousLocationInView:[self superview]]
andThisPoint:[secondTouch previousLocationInView:[self superview]]];
float ratio = newDistance / oldDistance;
CGAffineTransform scale = CGAffineTransformMakeScale(ratio, ratio);
[self setTransform: CGAffineTransformConcat([self transform], scale)];
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesEnded:touches withEvent:event];
}
- (float) distanceBetweenThisPoint:(CGPoint)firstPoint andThisPoint:(CGPoint)secondPoint
{
float xDif = firstPoint.x - secondPoint.x;
float yDif = firstPoint.y - secondPoint.y;
float dist = ((xDif * xDif) + (yDif * yDif));
return sqrt(dist);
}
- (float) angleBetweenThisPoint:(CGPoint)firstPoint andThisPoint:(CGPoint)secondPoint
{
float xDif = firstPoint.x - secondPoint.x;
float yDif = firstPoint.y - secondPoint.y;
return atan2(xDif, yDif);
}
// Check Position Attempts
/*
// No Error here, just doesn't work.
- (void)CheckPosition{
if (objCircleBlue.center.x && objCircleBlue.center.y == objCircleBlueHole.center.x && objCircleBlueHole.center.y)
{
UIAlertView *playerWin = [[UIAlertView alloc] initWithTitle:@"You Win" message:@"Yeahh you won" delegate:nil cancelButtonTitle:@"OKAY!" otherButtonTitles:nil];
[playerWin show];
}
}
*/
/* ERROR
- (void)CheckPosition{
if (objCircleBlue.x && objCircleBlue.y == objCircleBlueHole.x && objCircleBlueHole.y) // Request for member 'x' in something not a structure or union
{
UIAlertView *playerWin = [[UIAlertView alloc] initWithTitle:@"You Win" message:@"Yeahh you won" delegate:nil cancelButtonTitle:@"OKAY!" otherButtonTitles:nil];
[playerWin show];
}
}
*/
- (void)CheckPosition{
if (objCircleBlue.center.x == 15) // A friend suggested to try that to see if my win condition even works, NSLog didn't report it a win.
{
UIAlertView *playerWin = [[UIAlertView alloc] initWithTitle:@"You Win" message:@"Yeahh you won" delegate:nil cancelButtonTitle:@"OKAY!" otherButtonTitles:nil];
[playerWin show];
NSLog(@" WIN");
}
}
@end
Heres what I do know. I know that doing the .center.x and .center.y is a bad choice since that point alone is the size of a pixel so the player would have to be ridiculously accurate for it to work. I know that I should use an "area" of some sort (a rectangle most likely) to write a condition if the object is within a certain area, then win.
Heres what I don't know. I don't know if my win condition even works, it doesn't give me an error but I haven't seen it execute. It hasn't executed either due to it not working, or do to my condition statement to be wrong. One or the other is written improperly maybe both.
A little bit about the kind of game I am trying to accomplish. Its a little kids game, where I want to do basic things, like fitting objects in the right spots like a puzzle piece. Which is why I need to figure out a coordinate event of some sort. And the reason why I also included rotating and scaling is because later in the game for a "harder" difficulty I want the player to stretch or shrink the image to a certain size or rotate it to a certain degree to win. Its a very basic game I and my fellow students are trying to execute. Our coding team is myself and 2 other kids, and were all about as good as each other. The books I have and use are, Programming in Objective C 2.0 by Stephen Kochan and Learning iPhone Game Programming by Michael Daley. I was borrowing the Rory Lewis book from a friend. Thanks for you're help, I am looking through the books trying to come up with something. Their isn't a tutor at school for iPhone development but a friend told me about here. Between advice here, the books, self determination, I am sure I will get it done, I won't give up
Last edited by a moderator: