Hello, first of all thanks for the people who created the great framework used for developing game that is cocos2d.
I'm new in cocos2d and now I'm having trouble to retrieve/read the sprite coordinate x and y. To display the output of coordinate x and y, i used NSLog method to print it on console:
My sprites:
[URL=http://img20.imageshack.us/i/39935865.png/][IMG]http://img20.imageshack.us/img20/350/39935865.th.png[/IMG][/URL]
Here is the code:
// Needed for UIWindow, NSAutoReleasePool, and other objects
#import <UIKit/UIKit.h>
#include "chipmunk.h"
// Import the interfaces
#import "MoveDirection.h" //Fix here
// A simple define used a tag
enum {
kTagSprite = 1, kTagBat, ktagBall,
};
@implementation MoveDirection
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init] )) {
// isTouchEnabled is an property of Layer (the super class).
// When it is YES, then the touches will be enabled
self.isTouchEnabled = YES;
// isAccelerometerEnabled is property of Layer (the super class).
// When it is YES, then the accelerometer will be enabled
self.isAccelerometerEnabled = YES;
//
// Label
//
// create and initialize a Label
Label* label = [Label labelWithString:@"TIMUR, UTARA, BARAT, SELATAN" fontName:@"Marker Felt" fontSize:15];
// ask director the the window size
CGSize size = [[Director sharedDirector] winSize];
// position the label on the center of the screen
// "ccp" is a helper macro that creates a point. It means: "CoCos Point"
label.position= ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild: label];
//
// Sprite
//
Sprite *bg = [Sprite spriteWithFile:@"wallpaper-nightlyjourney1024.jpg"];
Sprite *circle = [Sprite spriteWithFile:@"OE.gif"];
Sprite *ball = [Sprite spriteWithFile:@"ball.png"];
Sprite *bat = [Sprite spriteWithFile:@"1R.gif"];
bg.position = ccp(size.width/2, size.height/2);
circle.position = ccp( 50,50);
donttouch.position = ccp(size.width/2,size.height/2);
bat.position = ccp(50,50);
// z is the z-order. Greater values means on top of lower values.
// Default z value is 0. So the sprite will be on top of the label.
// Add the sprite with a tag, so we can later 'get' the sprite by this tag
[self addChild:bg z:0];
[self addChild:circle z:1 tag:kTagSprite];
[self addChild:ball z:2 tag:kTagBall];
[self addChild:bat z:3 tag:kTagBat];
}
return self;
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//Kat sini determine siapa yg run
CocosNode *circle = [self getChildByTag:kTagSprite];
CocosNode *bat = [self getChildByTag:kTagBat];
CocosNode *ball = [self getChildByTag:ktagBall];
UITouch *touch = [touches anyObject];
if( touch ) {
NSLog(@"X coordinate: %d", bat.position.x); //TRY TO RETRIEVE VALUE OF COORDINATE X -PROBLEM IS HERE
NSLog(@"Y coordinate: %d", bat.position.y); //TRY TO RETRIEVE VALUE OF COORDINATE Y - PROBLEM IS HERE
CGPoint location = [touch locationInView: [touch view]];
// IMPORTANT:
// The touches are always in "portrait" coordinates. You need to convert them to your current orientation
CGPoint convertedPoint = [[Director sharedDirector] convertCoordinate:location];
// we stop the all running actions
//[circle stopAllActions];
// and we run a new action
[bat runAction: [JumpTo actionWithDuration:5 position:convertedPoint height:20 jumps:5]];
[circle runAction: [MoveTo actionWithDuration:1 position:convertedPoint]];
[ball runAction: [MoveTo actionWithDuration:1 position:convertedPoint]
// no other handlers will receive this event
return kEventHandled;
}
// we ignore the event. Other receivers will receive this event.
return kEventIgnored;
}
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration
{
CocosNode *circle = [self getChildByTag:kTagSprite];
// Convert the coordinates to 'landscape' coords
// since they are always in 'portrait' coordinates
CGPoint converted = ccp( (float)-acceleration.y, (float)acceleration.x);
// update the rotation based on the z-rotation
// the sprite will always be 'standing up'
circle.rotation = (float) CC_RADIANS_TO_DEGREES( atan2f( converted.x, converted.y) + M_PI );
// update the scale based on the length of the acceleration
// the higher the acceleration, the higher the scale factor
circle.scale = 0.5f + sqrtf( (converted.x * converted.x) + (converted.y * converted.y) );
}
@end
@implementation AppController
// window is a property. @synthesize will create the accesors methods
@synthesize window;
// Application entry point
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
// create an initilize the main UIWindow
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Enable Multiple Touches ? No
[window setMultipleTouchEnabled:NO];
// Attach cocos2d to the window
[[Director sharedDirector] attachInWindow:window];
// before creating any layer, set the landscape mode
[[Director sharedDirector] setDeviceOrientation:CCDeviceOrientationLandscapeLeft];
// Make the window visible
[window makeKeyAndVisible];
// Create and initialize parent and empty Scene
Scene *scene = [Scene node];
// Create and initialize our HelloEvents Layer
Layer *layer = [MoveDirection node];
// add our HelloEvents Layer as a child of the main scene
[scene addChild:layer];
// Run!
[[Director sharedDirector] runWithScene: scene];
}
- (void) dealloc
{
[window release];
[super dealloc];
}
@end
===================== END OF CODE=======================================
When i touched the screen, the bat will move to where the location i clicked and the problem is the value of location displayed using this code the above code is weird:
THE CODE TO PRINT THE VALUES:
NSLog(@"X coordinate: %d", circle.position.x);
NSLog(@"Y Coordinate: %d", circle.position.y);
Here is the screenshot on console:
[URL=http://img20.imageshack.us/i/96753811.png/][IMG]http://img20.imageshack.us/img20/6407/96753811.th.png[/IMG][/URL]
What I just want is the ordinary location as ccp method cpp(302,329);
I did google for this problem and I didn't find the solution