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 > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-12-2011, 03:21 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 176
Rifts is on a distinguished road
Default Can not comprehend how to separate accelerometer class

Hey All!

I'm trying to put my accelerometer into its own class but I can not comprehend how to do this.
If my code for my accelerometer is this.

Code:
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
	// controls how quickly velocity decelerates (lower = quicker to change direction)
	float deceleration = 0.4f;
	// determines how sensitive the accelerometer reacts (higher = more sensitive)
	float sensitivity = 6.0f;
	// how fast the velocity can be at most
	float maxVelocity = 100;
	// adjust velocity based on current accelerometer acceleration
	playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity;
	playerVelocity.y = playerVelocity.y * deceleration + acceleration.y * sensitivity;
	// we must limit the maximum velocity of the player sprite, in both directions
	if (playerVelocity.x > maxVelocity)
	{
		playerVelocity.x = maxVelocity;
	}
	else if (playerVelocity.x < - maxVelocity)
	{
		playerVelocity.x = - maxVelocity;
	}

	if (playerVelocity.y > maxVelocity)
	{
		playerVelocity.y = maxVelocity;
	}
	else if (playerVelocity.y < - maxVelocity)
	{
		playerVelocity.y = - maxVelocity;
	} 

}

-(void) gameOn:(ccTime)delta
{

	// Keep adding up the playerVelocity to the player's position
	CGPoint pos = player.position;
	pos.x -= playerVelocity.y;
	pos.y += playerVelocity.x;

	// The Player should also be stopped from going outside the screen
	CGSize screenSize = [[CCDirector sharedDirector] winSize];
	float imageWidthHalved = player.contentSize.width * 0.5f * 0.5f;
	float leftBorderLimit = imageWidthHalved;
	float rightBorderLimit = screenSize.width - imageWidthHalved;
	// preventing the player sprite from moving outside the screen left and right
	if (pos.x < leftBorderLimit)
	{
		pos.x = leftBorderLimit;
		playerVelocity = CGPointZero;
	}
	else if (pos.x > rightBorderLimit)
	{
		pos.x = rightBorderLimit;
		playerVelocity = CGPointZero;
	}
	float imageHeightHalved = [player texture].contentSize.height * 0.5f * 0.5f;
	float topBorderLimit = imageHeightHalved;
	float bottomBorderLimit = screenSize.height - imageHeightHalved;
	// preventing the player sprite from moving outside the screen top and btm
	if (pos.y < topBorderLimit)
	{
		pos.y = topBorderLimit;
		playerVelocity = CGPointZero;
	}
	else if (pos.y > bottomBorderLimit)
	{
		pos.y = bottomBorderLimit;
		playerVelocity = CGPointZero;
	}
	// assigning the modified position back
	player.position = pos;
I don't understand how to "link" it to my character because having this in its own class can not access the "playerVelocity" or "player" objects.

I hope that makes sense.

Thanks
Rifts is offline   Reply With Quote
Old 10-12-2011, 04:08 PM   #2 (permalink)
Programmer
 
Stinkee2's Avatar
 
Join Date: Nov 2010
Location: New York
Age: 17
Posts: 15
Stinkee2 is on a distinguished road
Default

I'm not familiar enough with Obj-C to give you any code, but just do something like this:

Make a class with floating point X, Y, and Z values, then create an instance of this class in your class there that has the didAccelerate and gameOn functions. Then in the didAccelerate function update the X, Y, and Z values of the class instance. Then just access them from wherever. Or you could go as far as putting a pointer to your class with the gameOn function in the accelerometer class and add an update function that modifies the player and playerVelocity variables. In C++ this would look something like this, I'm sure you'll be able to take something from it even if you don't know C++.

Code:
class Game; //Forward declaration
class AccelerationVector
    {
        public:
            void Update(Game *G)
                {
                     G->Player.Velocity += Vector3(X,Y,Z);
                     G->Player.Position += G->Player.Velocity;
                     //Whatever else you want to do with the player
                }
            float X,Y,Z;
    };

class Game
    {
        public:
            void didAccelerate(Acceleration)
                {
                     AccVec.SetAcceleration(Acceleration);
                }
            void gameOn()
                {
                     //Updates players velocity and position and stuff
                     AccVec.Update(this);
                }
            AccelerationVector AccVec;
    };
I'm not sure I understood your question though, I hope this helps.
__________________
This is a signature.
Stinkee2 is offline   Reply With Quote
Old 10-13-2011, 02:15 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 100
XcodeMagic is on a distinguished road
Default

go on youtube and type in "simple iphone game eagle11" he made a 10- part tutorial and it shows you the exact code to use to work the accelerometer in a way simpler way that you have.
XcodeMagic is offline   Reply With Quote
Reply

Bookmarks

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: 386
10 members and 376 guests
7twenty7, apatsufas, comicool, Creativ, Dalia, dansparrow, Duncan C, Murphy, pbart, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,916
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

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