I can not get the player position --

I tried to get the position of the character after each frame :(. I can not get the player position. in cclog returns an incorrect value.Below is my source code.
thank you!

http://www.mediafire.com/file/agsf6hfqeu0p7sj/New_folder_%286%29.zip/file

Please post your source directly. It isn’t safe for our users to blindly trust some random URL. Especially one that wants me to disable my blockers…

3 Likes

`I uploaded the file and received a notification :persevere::persevere::persevere:

Sorry, that file is too big (maximum size is 1024000kb). Why not upload your large file to a cloud sharing service, then share the link?`

@blackcat99 You do not need to post your entire project, just copy the section of code that is relevant to your question, and paste it in your post.

2 Likes

Post where your problem is and we can always ask for more @R101 has the right idea.

1 Like
#include "player.h"
#include <stdio.h>
#include <string>

using namespace std;
USING_NS_CC;

string s;
Label *myLabel;
Player::Player(Layer *layer)
{
	Vec2 origin = Director::getInstance()->getVisibleOrigin();
	auto visibleSize = Director::getInstance()->getVisibleSize();
	
	//idle animation 

	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("idle.plist", "idle.png");
	player = Sprite::createWithSpriteFrameName("id1.png");
	//player->setPosition(Vec2(visibleSize.height/2, visibleSize.width/2));
	player->setPosition(190, 190);
	player->setScale(2);
	Vector<SpriteFrame*> animFrames;
	animFrames.reserve(5);
	animFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("id2.png"));
	animFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("id3.png"));
	animFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("id4.png"));
	animFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("id5.png"));
	Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.5f);
	idle = Animate::create(animation);
	idle->retain();
	player->runAction(RepeatForever::create(idle));

	//run animation

	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("run.plist", "run.png");
	Vector<SpriteFrame*> animFrames1;
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r1.png"));
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r2.png"));
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r4.png"));
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r5.png"));
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r6.png"));
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r7.png"));
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r8.png"));
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r9.png"));
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r10.png"));
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r11.png"));
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r12.png"));
	animFrames1.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("r13.png"));
	Animation* animation1 = Animation::createWithSpriteFrames(animFrames1, 0.07f);
	run = Animate::create(animation1);
	run->retain();

	s = "HP: " + to_string(HP);
	myLabel = Label::createWithTTF(s, "/fonts/arial.ttf", 24);
	myLabel->setPosition(Vec2(500, 500));
	myLabel->setColor(Color3B::RED);
	layer->addChild(myLabel, 10000);
	

	auto playerbody = PhysicsBody::createBox(player->getContentSize());
	playerbody->setGravityEnable(false);
	playerbody->setCollisionBitmask(1);
	playerbody->setContactTestBitmask(true);
	player->setPhysicsBody(playerbody);
	
	layer->addChild(player, 100);

	

}
void Player::update()
{
	HP -= 100;
	s = "HP: " + to_string(HP);
	myLabel->setString(s);
	CCLOG("HP = %d", HP);
}
void Player::right()
{
	auto phai = MoveBy::create(20, Vec2(8000, 0));
	phai->setTag(1);
	player->setScaleX(2);
	player->stopAllActions();
	player->runAction(phai);
	player->runAction(RepeatForever::create(run));
}
void Player::left()
{
	auto trai = MoveBy::create(20, Vec2(-8000, 0));
	player->setScaleX(-2);
	player->stopAllActions();
	player->runAction(trai);
	player->runAction(RepeatForever::create(run));
	trai->setTag(2);
}
void Player::up()
{
	auto tren = MoveBy::create(20, Vec2(0, 8000));
	player->runAction(tren);
	tren->setTag(3);
}
void Player::down()
{
	auto duoi = MoveBy::create(20, Vec2(0, -8000));
	player->runAction(duoi);
	duoi->setTag(4);
}
void Player::stopactions()
{
	/*player->stopActionByTag(1);
	player->stopActionByTag(2);
	player->stopActionByTag(3);
	player->stopActionByTag(4);*/
	player->stopAllActions();
	player->runAction(RepeatForever::create(idle));
}
void Player::updateposition()
{
	auto x = player->getPositionX();
	auto y = player->getPositionY();
	CCLOG("%d - %d", x, y);
}

This is the code of player.cpp.

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include <stdio.h>
#include <string>


USING_NS_CC;
using namespace ui;
using namespace std;
Node *node;



Scene* HelloWorld::createScene()
{
	auto scene = Scene::createWithPhysics();
	scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); // tao vien cho physicsbody


	auto layer = HelloWorld::create();
	layer->SetPhysicsWorld(scene->getPhysicsWorld());

	scene->addChild(layer);

	return scene;
}

bool HelloWorld::init()
{
	if (!Layer::init())
	{
		return false;
	}
	Vec2 origin = Director::getInstance()->getVisibleOrigin();
	auto visibleSize = Director::getInstance()->getVisibleSize();

	/*auto background = Sprite::create("background.jpg");
	background->setPosition(visibleSize / 1.5);
	this->addChild(background,1);*/

	auto xuongrong = Sprite::create("xuongrong.png");
	xuongrong->setPosition(500,190);
	this->addChild(xuongrong, 2);
	auto xuongrongbody = PhysicsBody::createBox(xuongrong->getContentSize());
	xuongrongbody->setGravityEnable(false);
	xuongrongbody->setDynamic(false);
	xuongrongbody->setCollisionBitmask(2);
	xuongrongbody->setContactTestBitmask(true);
	xuongrong->setPhysicsBody(xuongrongbody);

	auto body = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3);
	auto edgenode = Node::create();
	edgenode->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
	edgenode->setPhysicsBody(body);
	body->setCollisionBitmask(2);
	body->setContactTestBitmask(true);
	addChild(edgenode);

	player = new Player(this);
	//schedule(schedule_selector(HelloWorld::update), 1);

	auto listener = EventListenerKeyboard::create();
	listener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onkeypress, this);
	listener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onkeyrelease, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

	// kiem tra va cham.
	auto contactlisterner = EventListenerPhysicsContact::create();
	contactlisterner->onContactBegin = CC_CALLBACK_1(HelloWorld::OnPhysicsContact, this);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(contactlisterner, this);

	return true;
}
void HelloWorld::update(float dt)
{
	player->updatevitri();
}
void HelloWorld::onkeypress(EventKeyboard::KeyCode keycode, Event *event)
{
	CCLOG("press key %d press ", keycode);
	
	if (keycode == EventKeyboard::KeyCode::KEY_D)
	{
		player->phai();
	}
	else if (keycode == EventKeyboard::KeyCode::KEY_A)
	{
		player->trai();
	}
	else if (keycode == EventKeyboard::KeyCode::KEY_W)
	{
		player->tren();
	}
	else if (keycode == EventKeyboard::KeyCode::KEY_S)
	{
		player->duoi();
	}
	else if (keycode == EventKeyboard::KeyCode::KEY_F)
	{
		fire();
	}
	
}
void HelloWorld::onkeyrelease(EventKeyboard::KeyCode keycode, Event *event)
{
	player->dungHD();
}
bool HelloWorld::OnPhysicsContact(PhysicsContact &contact)
{
	auto objecA = contact.getShapeA()->getBody();
	auto objectB = contact.getShapeB()->getBody();

	if ( objecA->getCollisionBitmask() == 1 && objectB->getCollisionBitmask() == 2 || objecA->getCollisionBitmask() == 2 && objectB->getCollisionBitmask() == 1)
	{
		CCLOG("trigger");
		player->update();
	}
	return true;
}

helloworld.cpp.

The position of the player when the cclog is out is incorrect .
thank you

What do you get as the output from CCLOG for the player position? Where is player->updatePosition() called from?

I only have the jaw moved from helloworld.cpp then I called the moveby to move the player.I do not have the function to update the position of the player

That’s not what I was asking.

Firstly, what output do you get from CCLOG? Post the actual values you’re seeing.

Also, looking at the section of code you posted:

void Player::updateposition()
{
	auto x = player->getPositionX();
	auto y = player->getPositionY();
	CCLOG("%d - %d", x, y);
}

Where do you call that specific method from?

Here’s a small suggestion: The way your code is structured makes things harder to follow. You have a Player class that contains a member named “player”, yet that is actually a sprite. You then create a new instance of the Player class that you again name “player”. You can see how things like this may make it harder to understand and maintain. Perhaps you should name the sprite “playerSprite”, which would at least differentiate it from instances of the Player class.

In Node class:

virtual float getPositionX(void) const;

So trace float as integers is incorrect CCLOG("%d - %d", x, y);

1 Like