運行了教程的示例程式,點擊螢屏任意一個位置會生成一個小球,自由下落,螢屏是邊框。
然后問題出現了,小球落到地方靜止后,隔一會就會亂跳,有時跳起來的速度很慢,有時很快,甚至快到直接彈出邊框。
百度了好久都沒找到解決方法,請問是什么問題呢。
附上原始碼。
HelloWorld.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setGravity(Vect(0.0f, -100.0f));
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
// 創建一個精靈
auto edgeSp = Sprite::create();
// 創建一個靜態四邊形剛體,表示螢屏四邊
auto body = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3);
edgeSp->setPosition(visibleSize.width / 2, visibleSize.height / 2);
edgeSp->setPhysicsBody(body);
this->addChild(edgeSp);
std::vector<std::string> imageNames;
imageNames.push_back("basketball.png");
imageNames.push_back("football.png");
auto planeListener = EventListenerTouchOneByOne::create();
planeListener->onTouchBegan = [](Touch* touch, Event* event){return true; };
planeListener->onTouchEnded = [=](Touch* touch, Event* event)
{
Vec2 touchLocation = touch->getLocation();
Vec2 nodeLocation = this->convertToNodeSpace(touchLocation);
int rand = random() % 2;
std::string imageName = imageNames.at(rand);
Sprite* ball = Sprite::create(imageName);
ball->setPosition(nodeLocation);
auto body = PhysicsBody::createCircle(ball->getContentSize().width / 2);
body->setVelocity(Vect(0.0f, -100.0f));
PhysicsShape* ps = body->getShape(0);
ps->setMoment(0.2);
ps->setMass(1.0f);
ps->setDensity(0.2f);
ps->setFriction(0);
ps->setRestitution(0);
ball->setPhysicsBody(body);
this->addChild(ball);
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(planeListener, this);
return true;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
uj5u.com熱心網友回復:
這個問題是幀頻與物理模擬步長不一致導致的,一般會出現在剛初始化界面卡幀的時候。如果你的機器快,就不會出現。解決方法
1關閉自動模擬
scene->getPhysicsWorld()->setAutoStep(false);
2在所屬的update方法里添加如下內容
if (dt > 0.016f && dt < 0.025f)
{
scene->getPhysicsWorld()->step(dt);//dt是方法 update(flaot dt)的引數
}
以上內容中的(dt > 0.016f && dt < 0.025f)表示 只有在幀頻穩定在這個范圍的情況下才進行物理模擬。
具體幀頻范圍可以根據自己需求調整,一般幀頻都是在1/60(0.016)左右浮動,如果浮動過大,證明卡幀了,那么就不進行物理模擬,這樣就防止了自動彈跳的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/72623.html
標籤:Cocos2d-x
