Topic:   My iPhone Game Tutorials for Beginners   (Read 320211 times)


0 Members and 3 Guests are viewing this topic.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #300 on: December 16, 2011, 11:28:49 PM »
Delete any code involved with the NSRunLoop. That's unnecessary. Set the target of the NSTimer's scheduledTimer.... to "self" without the quotations.

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: My iPhone Game Tutorials for Beginners
« Reply #301 on: December 18, 2011, 06:08:59 PM »
It worked! thanksĀ  :) It's weird that the apple documentation would go into the whole mess of NSRunLoop when you could make it as simple as your method.Ā  [smiley=dankk2.gif]
« Last Edit: December 18, 2011, 06:09:12 PM by KurtManion »
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: My iPhone Game Tutorials for Beginners
« Reply #302 on: December 21, 2011, 09:01:12 PM »
My device orientation is LandscapeRight. My UITouch event still thinks the iPod is in portrait. Is there an actual way to tell the UITouch that the device has changed orientation, or do I just have to do some subtraction? It seems like there should be some method to switch it's orientation, but I can't find it's documentation anywhere.
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #303 on: December 21, 2011, 09:10:19 PM »
UIViews contain the UITouch responders. A UIView's rotation depends on the rotation of the ViewController. If the ViewController is in portrait, the UIView will be in portrait, which means the touches will be in portrait.

Make sure you set your device orientations in the UIViewController:
[code Objective C]-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}[/code]

Also, you can set the landscape of a UIViewController within Xcode's visual editor. I think you can also set the window's orientation...

Oh and yeah, in your application's PLIST, there should be an option to specify the starting orientation of your application.
« Last Edit: December 21, 2011, 09:11:15 PM by Gandolf »

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: My iPhone Game Tutorials for Beginners
« Reply #304 on: January 02, 2012, 02:08:16 PM »
So I've been trying to get the sprite to move, and it sort of works, it moves up if you touch the top, and the other sides, don't really work coorectly, or at all. Thats not the real problem though. A lot of times I touch the screen, and the movement method is called, but isn't allowed to continue, like it's being cut off by the gametimer calling it again. At least I think thats whats wrong, but I have no idea, and haven't been able to find any solutions online

http://www.mediafire.com/?ufo745pt3tb715x
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #305 on: January 02, 2012, 03:07:57 PM »
Sorry Kurt, turns out I haven't installed Xcode yet on my new computer.

I'm on the road and it'll be tonight before I can try it out. In the meantime I'll use TextEdit to browse your code...

Edit:
Your code looks good, I can clean up a bunch of it. I don't immediately see anything that could cause problems. I'm gonna have to test later when I install Xcode.

Edit 2:
On second thought, your code looks kinda strange. I'm gonna edit and post the new version.
« Last Edit: January 02, 2012, 03:09:33 PM by Gan »

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #306 on: January 02, 2012, 03:26:35 PM »
AdventureViewController.h
Code: [Select]
//
//  AdventureViewController.h
//  Adventure
//
//  Created by Kurt Manion on 12/10/11.
//  Copyright 2011 __Compleatly_Coded__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AdventureViewController : UIViewController {
//I like to leave the first letter lowercase.
//I have uppercase for class names
NSTimer* gameTimer;
CGPoint  location;
BOOL touching;
UIImageView* hero;
UIImageView* stage;
}

- (void) makeTimer;
- (void) movehero;

/*
Unnecessary. Only use if another class needs to acess these variables
@property (nonatomic, retain) NSTimer* GameTimer;
@property (nonatomic, readwrite) CGPoint Location;
*/

@end


AdventureViewController.m
Code: [Select]
//
//  AdventureViewController.m
//  Adventure
//
//  Created by Kurt Manion on 12/10/11.
//  Copyright 2011 __Compleatly_Coded__. All rights reserved.
//

#import "Adventure_Prefix.pch"
#import "AdventureViewController.h"

@implementation AdventureViewController

/*
@synthesize GameTimer;
@synthesize Location;
*/

#pragma mark -
#pragma mark Touches

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

NSSet* allTouches = [event allTouches];
UITouch* touch = [[allTouches allObjects] objectAtIndex:0];
CGPoint touchpoint = [touch locationInView:self.view];

touching = YES;
touchpoint.x = [touch locationInView:self.view].y;
touchpoint.y = 320 - [touch locationInView:self.view].x;

location = touchpoint;


NSLog(@"touch at location:%@",NSStringFromCGPoint(location));

}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

//[GameTimer invalidate];
touching = NO;
//NSLog(@"Touches ended");

}


#pragma mark -
#pragma mark CustomInitalizer

// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
touching = NO;
[self makeTimer];

stage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stage1.png"]];
hero = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Sprite.png"]];
[self.view addSubview:stage];
[self.view addSubview:hero];
//[self.view insertSubview:Stage atIndex:0];
//[self.view insertSubview:Hero atIndex:1];
Stage.center = CGPointMake(480/2,320/2);
Hero.center = CGPointMake(480/2,320/2);

/*
Allocating automatically retains.
[Sprite retain];
[Hero retain];
[Stage1 retain];
[Stage retain];
*/

    }
    return self;
}

- (void)dealloc {
    [super dealloc];
[GameTimer release];
}

#pragma mark -
#pragma mark SetUpTimer

- (void) makeTimer {

NSLog(@"making timer");
gameTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
   selector:@selector(movehero)
   userInfo:nil
repeats:YES];
}

- (void) movehero {

if (touching == YES)
{
NSLog(@"entering move hero");


NSLog(@"%@",NSStringFromCGPoint(hero.center));

//CGPoint newCenter = Hero.center;

//What is MVM? I assume movement speed
//I'd suggest declaring it as a var or using a number
int MVM = 5;

if (location.x >= 240) {NSLog(@"touch at top"); hero.center = CGPointMake(hero.center.x - MVM, hero.center.y);}
if (location.x <= 80) {NSLog(@"touch at bottom");hero.center = CGPointMake(hero.center.x + MVM, hero.center.y);}
if (location.y >= 360) {NSLog(@"touch at right");hero.center = CGPointMake(hero.center.x, hero.center.y + MVM);}
if (location.y <= 120) {NSLog(@"touch at left");hero.center = CGPointMake(hero.center.x, hero.center.y - MVM);}

//hero.center=newCenter;
/*
Already in the view, don't need to re-insert
[self.view insertSubview:Hero atIndex:1];
*/
//NSLog(@"moving to:%@", NSStringFromCGPoint(newCenter));

}
}


/*
// 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];
}
*/

#pragma mark -
#pragma mark CustomRules

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

/*
- (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;
}
*/


@end

Cleaned up your code, might have fixed up some bugs. Can't test yet.

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #307 on: January 02, 2012, 07:15:18 PM »
Hey Kurt, installed Xcode and took a look at your code.

There was a lot messed up.

http://cl.ly/0j000n2B3o1z1Z0Z3I0k

Also, I believe I fixed the UIView rotation thingymagig touch coordinates. Also added the TouchesMoved method. Ah and fixed the movement. Hmmm....
Have fun. Feel free to post questions and code whenever you run into a problem. I like checking out other people's code and fixing stuff up.

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: My iPhone Game Tutorials for Beginners
« Reply #308 on: January 03, 2012, 04:40:55 PM »
Thanks Gan  ;D, yeah the code was really messed up, but I see what I did wrong.

The touch glitch is still there though. If I touch the screen, and lift before the timer executes the character doesn't move. I've got 2 ideas I'm going to try out after homework.

Of yeah 'MVM' was a macro, it's located in the Adventure_Prefix.pch, along with 'CENTER'
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #309 on: January 03, 2012, 09:12:48 PM »
Oh haha, that's an easy fix.

Put this at the end of the TouchesBegan function:
Code: [Select]
[gameTimer fire];

When you touch, the timer fires immediately and the character moves. Before, you had it wait up to 0.2 seconds before anything happened.

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: My iPhone Game Tutorials for Beginners
« Reply #310 on: January 04, 2012, 04:38:52 PM »
oh thats much nicer than what I did, I had it calling movehero outside of the timer. Cool thanks Gan, I'm back on track  ;D
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: My iPhone Game Tutorials for Beginners
« Reply #311 on: January 06, 2012, 10:07:25 PM »
The links in your first post (ball tutorial) go to a "file not found" error.
Did you update them? Did I just not read enough pages? ???
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #312 on: January 06, 2012, 10:30:54 PM »
Hmmm. I think those files disappeared somewhere. I can post a beginner template if you want to give it a try.

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
Re: My iPhone Game Tutorials for Beginners
« Reply #313 on: January 13, 2012, 02:05:12 PM »
Whenever I open up Xcode after having quit it, It sets the active executable to iPad, is there any way to make the default iPhone?
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: My iPhone Game Tutorials for Beginners
« Reply #314 on: January 13, 2012, 02:36:12 PM »
That's a good question. I have no idea.

My suggestion, go play around in your project setting.