void AccelerometerTest::didAccelerate(CCAcceleration* pAccelerationValue) { // double fNow = pAccelerationValue->timestamp; // // if (m_fLastTime > 0.0) // { // CCPoint ptNow = convertToUI // } // // m_fLastTime = fNow; CCDirector* pDir = CCDirector::sharedDirector(); /*FIXME: Testing on the Nexus S sometimes m_pBall is NULL */ if ( m_pBall == NULL ) { return; } CCSize ballSize = m_pBall->getContentSize(); CCPoint ptNow = m_pBall->getPosition(); CCPoint ptTemp = pDir->convertToUI(ptNow); ptTemp.x += pAccelerationValue->x * 9.81f; ptTemp.y -= pAccelerationValue->y * 9.81f; CCPoint ptNext = pDir->convertToGL(ptTemp); FIX_POS(ptNext.x, (VisibleRect::left().x+ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0)); FIX_POS(ptNext.y, (VisibleRect::bottom().y+ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0)); m_pBall->setPosition(ptNext); }
void StartLayer::update(float dt) { CCSprite* title = (CCSprite*)this->getChildByTag(10000); CCDirector* pDir = CCDirector::sharedDirector(); CCSize winSize = pDir->getWinSize(); //判断小球精灵是否有效。 if ( title == NULL ) { return; } //取得小球的图像区域大小。 CCSize ballSize = title->getContentSize(); //取得小球的当前位置。 CCPoint ptNow = title->getPosition(); //将当前位置转换成界面坐标系的位置。 CCPoint ptTemp = pDir->convertToUI(ptNow); //由收到的速度乘以一个系数后来影响位置。 ptTemp.x += posChange.x; ptTemp.y -= posChange.y; //再转换为OPENGL坐标系的位置。貌似有点麻烦,其实直接在上面X,Y的加减上做上正确的方向即可。 CCPoint ptNext = pDir->convertToGL(ptTemp); //限定位置的X,Y的有效范围,等于小球边缘始终在窗口内。 FIX_POS(ptNext.x, (ballSize.width / 2.0), (winSize.width - ballSize.width / 2.0)); FIX_POS(ptNext.y, (ballSize.height / 2.0), (winSize.height - ballSize.height / 2.0)); //将位置传给小球。 title->setPosition(ptNext); }
void ShootingGameScene::didAccelerate(CCAcceleration* pAccelerationValue) { CCDirector* pDir = CCDirector::sharedDirector(); if (_aim == NULL) return; CCSize aimsize = _aim->getContentSize(); CCPoint aimNow = _aim->getPosition(); CCPoint aimTemp = pDir->convertToUI(aimNow); aimTemp.x += pAccelerationValue->x * 50.0f; aimTemp.y -= pAccelerationValue->y * 50.0f;//9.81f; CCPoint aimNext = pDir->convertToGL(aimTemp); FIX_POS(aimNext.x,(aimsize.width/2.0),(winsize.width - aimsize.width/2.0f)); FIX_POS(aimNext.y,(aimsize.height/2.0),(winsize.height - aimsize.height/2.0f)); _aim->setPosition(aimNext); }
void Director1::ccTouchesEnded(CCSet * touches, CCEvent* event) { CCSetIterator it; CCTouch* touch; for( it = touches->begin(); it != touches->end(); it++) { touch = (CCTouch*)(*it); if(!touch) break; CCPoint a = touch->locationInView(); CCDirector *director = CCDirector::sharedDirector(); CCPoint b = director->convertToUI(director->convertToGL(a)); CCLog("(%d,%d) == (%d,%d)", (int) a.x, (int)a.y, (int)b.x, (int)b.y ); } }