// CCTMXLayer - obtaining tiles/gids CCSprite * CCTMXLayer::tileAt(const CCPoint& pos) { CCAssert( pos.x < m_tLayerSize.width && pos.y < m_tLayerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position"); CCAssert( m_pTiles && m_pAtlasIndexArray, "TMXLayer: the tiles map has been released"); CCSprite *tile = NULL; unsigned int gid = this->tileGIDAt(pos); // if GID == 0, then no tile is present if( gid ) { int z = (int)(pos.x + pos.y * m_tLayerSize.width); tile = (CCSprite*) this->getChildByTag(z); // tile not created yet. create it if( ! tile ) { CCRect rect = m_pTileSet->rectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); tile = new CCSprite(); tile->initWithTexture(this->getTexture(), rect); tile->setBatchNode(this); tile->setPosition(positionAt(pos)); tile->setVertexZ((float)vertexZForPos(pos)); tile->setAnchorPoint(CCPointZero); tile->setOpacity(m_cOpacity); unsigned int indexForZ = atlasIndexForExistantZ(z); this->addSpriteWithoutQuad(tile, indexForZ, z); tile->release(); } } return tile; }
// used only when parsing the map. useless after the map was parsed // since lot's of assumptions are no longer true Sprite * TMXLayer::appendTileForGID(int gid, const Point& pos) { if (gid != 0 && (static_cast<int>((gid & kFlippedMask)) - _tileSet->_firstGid) >= 0) { Rect rect = _tileSet->rectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); intptr_t z = (intptr_t)(pos.x + pos.y * _layerSize.width); Sprite *tile = reusedTileWithRect(rect); setupTileSprite(tile ,pos ,gid); // optimization: // The difference between appendTileForGID and insertTileforGID is that append is faster, since // it appends the tile at the end of the texture atlas ssize_t indexForZ = _atlasIndexArray->num; // don't add it using the "standard" way. insertQuadFromSprite(tile, indexForZ); // append should be after addQuadFromSprite since it modifies the quantity values ccCArrayInsertValueAtIndex(_atlasIndexArray, (void*)z, indexForZ); return tile; } return nullptr; }
// TMXLayer - obtaining tiles/gids Sprite * TMXLayer::getTileAt(const Point& pos) { CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position"); CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released"); Sprite *tile = nullptr; int gid = this->getTileGIDAt(pos); // if GID == 0, then no tile is present if (gid) { int z = (int)(pos.x + pos.y * _layerSize.width); tile = static_cast<Sprite*>(this->getChildByTag(z)); // tile not created yet. create it if (! tile) { Rect rect = _tileSet->rectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); tile = Sprite::createWithTexture(this->getTexture(), rect); tile->setBatchNode(this); tile->setPosition(getPositionAt(pos)); tile->setVertexZ((float)getVertexZForPos(pos)); tile->setAnchorPoint(Point::ZERO); tile->setOpacity(_opacity); ssize_t indexForZ = atlasIndexForExistantZ(z); this->addSpriteWithoutQuad(tile, static_cast<int>(indexForZ), z); } } return tile; }
void Factory::initSpriteFrame(){ auto texture = Director::getInstance()->getTextureCache()->addImage("Monster.png"); monsterDead.reserve(4); for (int i = 0; i < 4; i++) { auto frame = SpriteFrame::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(258-48*i,0,42,42))); monsterDead.pushBack(frame); } }
cocos2d::Animation * BaseSprite::createAnimation(const char* formatStr, float width, float height, int frameCount, float f) { cocos2d::Vector<SpriteFrame*> frames; frames.reserve(frameCount); auto texture = Director::getInstance()->getTextureCache()->addImage(formatStr); width = width / frameCount; for (int i = 0; i < frameCount; i++) { auto frame = SpriteFrame::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(width * i, 0, width, height))); frames.pushBack(frame); } return Animation::createWithSpriteFrames(frames, f); }
bool CCSpriteFrame::initWithTextureFilename(const char* filename, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize) { m_pobTexture = NULL; m_strTextureFilename = filename; m_obRectInPixels = rect; m_obRect = CC_RECT_PIXELS_TO_POINTS( rect ); m_obOffsetInPixels = offset; m_obOffset = CC_POINT_PIXELS_TO_POINTS( m_obOffsetInPixels ); m_obOriginalSizeInPixels = originalSize; m_obOriginalSize = CC_SIZE_PIXELS_TO_POINTS( m_obOriginalSizeInPixels ); m_bRotated = rotated; return true; }
bool SpriteFrame::initWithTextureFilename(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize) { _texture = nullptr; _textureFilename = filename; _rectInPixels = rect; _rect = CC_RECT_PIXELS_TO_POINTS( rect ); _offsetInPixels = offset; _offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels ); _originalSizeInPixels = originalSize; _originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels ); _rotated = rotated; return true; }
bool SpriteFrame::initWithTextureFilename(const char* filename, const Rect& rect, bool rotated, const Point& offset, const Size& originalSize) { _texture = NULL; _textureFilename = filename; _rectInPixels = rect; _rect = CC_RECT_PIXELS_TO_POINTS( rect ); _offsetInPixels = offset; _offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels ); _originalSizeInPixels = originalSize; _originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels ); _rotated = rotated; return true; }
void TMXLayer::setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags flags) { CCASSERT(tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position"); CCASSERT(_tiles, "TMXLayer: the tiles map has been released"); CCASSERT(gid == 0 || gid >= _tileSet->_firstGid, "TMXLayer: invalid gid" ); TMXTileFlags currentFlags; int currentGID = getTileGIDAt(tileCoordinate, ¤tFlags); if (currentGID == gid && currentFlags == flags) return; int gidAndFlags = gid | flags; // setting gid=0 is equal to remove the tile if (gid == 0) { removeTileAt(tileCoordinate); } // empty tile. create a new one else if (currentGID == 0) { int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; setFlaggedTileGIDByIndex(z, gidAndFlags); } // modifying an existing tile with a non-empty tile else { int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; auto it = _spriteContainer.find(z); if (it != _spriteContainer.end()) { Sprite *sprite = it->second.first; Rect rect = _tileSet->getRectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); sprite->setTextureRect(rect, false, rect.size); this->reorderChild(sprite, z); if (flags) { setupTileSprite(sprite, sprite->getPosition(), gidAndFlags); } it->second.second = gidAndFlags; } else { setFlaggedTileGIDByIndex(z, gidAndFlags); } } }
void TMXLayer::setTileGID(int gid, const Point& pos, ccTMXTileFlags flags) { CCASSERT(pos.x < _layerSize.width && pos.y < _layerSize.height && pos.x >=0 && pos.y >=0, "TMXLayer: invalid position"); CCASSERT(_tiles && _atlasIndexArray, "TMXLayer: the tiles map has been released"); CCASSERT(gid == 0 || gid >= _tileSet->_firstGid, "TMXLayer: invalid gid" ); ccTMXTileFlags currentFlags; int currentGID = getTileGIDAt(pos, ¤tFlags); if (currentGID != gid || currentFlags != flags) { int gidAndFlags = gid | flags; // setting gid=0 is equal to remove the tile if (gid == 0) { removeTileAt(pos); } // empty tile. create a new one else if (currentGID == 0) { insertTileForGID(gidAndFlags, pos); } // modifying an existing tile with a non-empty tile else { int z = pos.x + pos.y * _layerSize.width; Sprite *sprite = static_cast<Sprite*>(getChildByTag(z)); if (sprite) { Rect rect = _tileSet->rectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); sprite->setTextureRect(rect, false, rect.size); if (flags) { setupTileSprite(sprite, sprite->getPosition(), gidAndFlags); } _tiles[z] = gidAndFlags; } else { updateTileForGID(gidAndFlags, pos); } } } }
void CCSprite::setTextureRectInPixels(CGRect rect, bool rotated, CGSize size) { m_obRectInPixels = rect; m_obRect = CC_RECT_PIXELS_TO_POINTS(rect); m_bRectRotated = rotated; setContentSizeInPixels(size); updateTextureCoords(m_obRectInPixels); CGPoint relativeOffsetInPixels = m_obUnflippedOffsetPositionFromCenter; // issue #732 if (m_bFlipX) { relativeOffsetInPixels.x = -relativeOffsetInPixels.x; } if (m_bFlipY) { relativeOffsetInPixels.y = -relativeOffsetInPixels.y; } m_obOffsetPositionInPixels.x = relativeOffsetInPixels.x + (m_tContentSizeInPixels.width - m_obRectInPixels.size.width) / 2; m_obOffsetPositionInPixels.y = relativeOffsetInPixels.y + (m_tContentSizeInPixels.height - m_obRectInPixels.size.height) / 2; // rendering using SpriteSheet if (m_bUsesBatchNode) { // update dirty_, don't update recursiveDirty_ m_bDirty = true; } else { // self rendering // Atlas: Vertex float x1 = 0 + m_obOffsetPositionInPixels.x; float y1 = 0 + m_obOffsetPositionInPixels.y; float x2 = x1 + m_obRectInPixels.size.width; float y2 = y1 + m_obRectInPixels.size.height; // Don't update Z. m_sQuad.bl.vertices = vertex3(x1, y1, 0); m_sQuad.br.vertices = vertex3(x2, y1, 0); m_sQuad.tl.vertices = vertex3(x1, y2, 0); m_sQuad.tr.vertices = vertex3(x2, y2, 0); } }
bool SpriteFrame::initWithTextureFilename(const std::string& filename, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize) { if (FileUtils::getInstance()->isFileExist(filename)) { _texture = nullptr; _textureFilename = filename; _rectInPixels = rect; _rect = CC_RECT_PIXELS_TO_POINTS( rect ); _offsetInPixels = offset; _offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels ); _originalSizeInPixels = originalSize; _originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels ); _rotated = rotated; _anchorPoint = Vec2(NAN, NAN); _centerRect = Rect(NAN, NAN, NAN, NAN); return true; } return false; }
bool CCSpriteFrame::initWithTexture(CCTexture2D* pobTexture, CCRect rect, bool rotated, CCPoint offset, CCSize originalSize) { m_pobTexture = pobTexture; if (pobTexture) { pobTexture->retain(); } m_obRectInPixels = rect; m_obRect = CC_RECT_PIXELS_TO_POINTS(rect); m_bRotated = rotated; m_obOffsetInPixels = offset; m_obOriginalSizeInPixels = originalSize; return true; }
bool CCSpriteFrame::initWithTexture(CCTexture2D* pobTexture, const CCRect& rect, bool rotated, const CCPoint& offset, const CCSize& originalSize) { m_pobTexture = pobTexture; if (pobTexture) { CC_SAFE_RETAIN(pobTexture); } m_obRectInPixels = rect; m_obRect = CC_RECT_PIXELS_TO_POINTS(rect); m_obOffsetInPixels = offset; m_obOffset = CC_POINT_PIXELS_TO_POINTS( m_obOffsetInPixels ); m_obOriginalSizeInPixels = originalSize; m_obOriginalSize = CC_SIZE_PIXELS_TO_POINTS( m_obOriginalSizeInPixels ); m_bRotated = rotated; return true; }
bool SpriteFrame::initWithTexture(Texture2D* texture, const Rect& rect, bool rotated, const Vec2& offset, const Size& originalSize) { _texture = texture; if (texture) { texture->retain(); } _rectInPixels = rect; _rect = CC_RECT_PIXELS_TO_POINTS(rect); _offsetInPixels = offset; _offset = CC_POINT_PIXELS_TO_POINTS( _offsetInPixels ); _originalSizeInPixels = originalSize; _originalSize = CC_SIZE_PIXELS_TO_POINTS( _originalSizeInPixels ); _rotated = rotated; return true; }
// CCTMXLayer - adding helper methods CCSprite * CCTMXLayer::insertTileForGID(unsigned int gid, const CCPoint& pos) { CCRect rect = m_pTileSet->rectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); intptr_t z = (intptr_t)(pos.x + pos.y * m_tLayerSize.width); // if quad optimization is used CCSprite *tile; if(m_pAtlasIndexArray) { tile = reusedTileWithRect(rect); setupTileSprite(tile, pos, gid); // get atlas index unsigned int indexForZ = atlasIndexForNewZ(z); // Optimization: add the quad without adding a child this->insertQuadFromSprite(tile, indexForZ); // insert it into the local atlasindex array ccCArrayInsertValueAtIndex(m_pAtlasIndexArray, (void*)z, indexForZ); // update possible children if (m_pChildren && m_pChildren->count()>0) { CCObject* pObject = NULL; CCARRAY_FOREACH(m_pChildren, pObject) { CCSprite* pChild = (CCSprite*) pObject; if (pChild) { unsigned int ai = pChild->getAtlasIndex(); if ( ai >= indexForZ ) { pChild->setAtlasIndex(ai+1); } } } }
bool Enemy::init() { bool ret = false; do { auto texture = Director::getInstance()->getTextureCache()->addImage("Enemy_walk.png"); auto frame0 = SpriteFrame::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(0, 0, 114, 110))); CC_BREAK_IF(!this->initWithSpriteFrame(frame0)); Animation* idleAnim = this->createAnimation("Enemy_idle.png", 206, 100, 2, 0.3); this->setIdleAction(RepeatForever::create(Animate::create(idleAnim))); Animation* walkAnim = this->createAnimation("Enemy_walk.png", 412, 100, 4, 0.1); this->setWalkAction(RepeatForever::create(Animate::create(walkAnim))); Animation* attackAnim = this->createAnimation("Enemy_attack.png", 515, 100, 5, 0.08); this->setAttackAction(Sequence::create(Animate::create(attackAnim), BaseSprite::createIdleCallbackFunc(), NULL)); Animation* hurtAnim = this->createAnimation("Enemy_idle.png", 228, 110, 2, 0.3); // 因为没有图,所以还没实现 this->setHurtAction(Sequence::create(Animate::create(hurtAnim), BaseSprite::createIdleCallbackFunc(), NULL)); Animation* deadAnim = this->createAnimation("Enemy_idle.png", 228, 110, 2, 0.3); // 因为没有图,所以还没实现 this->setDeadAction(Sequence::create(Animate::create(deadAnim), Blink::create(3, 9), NULL)); ret = true; } while (0); return ret; }
// TMXLayer - adding helper methods Sprite * TMXLayer::insertTileForGID(int gid, const Point& pos) { if (gid != 0 && (static_cast<int>((gid & kFlippedMask)) - _tileSet->_firstGid) >= 0) { Rect rect = _tileSet->rectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); intptr_t z = (intptr_t)(pos.x + pos.y * _layerSize.width); Sprite *tile = reusedTileWithRect(rect); setupTileSprite(tile, pos, gid); // get atlas index ssize_t indexForZ = atlasIndexForNewZ(static_cast<int>(z)); // Optimization: add the quad without adding a child this->insertQuadFromSprite(tile, indexForZ); // insert it into the local atlasindex array ccCArrayInsertValueAtIndex(_atlasIndexArray, (void*)z, indexForZ); // update possible children for(const auto &child : _children) { Sprite* sp = static_cast<Sprite*>(child); ssize_t ai = sp->getAtlasIndex(); if ( ai >= indexForZ ) { sp->setAtlasIndex(ai+1); } } _tiles[z] = gid; return tile; } return nullptr; }
// removing / getting tiles Sprite* TMXLayer::getTileAt(const Vec2& tileCoordinate) { CCASSERT( tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position"); CCASSERT( _tiles, "TMXLayer: the tiles map has been released"); Sprite *tile = nullptr; int gid = this->getTileGIDAt(tileCoordinate); // if GID == 0, then no tile is present if( gid ) { int index = tileCoordinate.x + tileCoordinate.y * _layerSize.width; auto it = _spriteContainer.find(index); if (it != _spriteContainer.end()) { tile = it->second.first; } else { // tile not created yet. create it Rect rect = _tileSet->getRectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); tile = Sprite::createWithTexture(_texture, rect); Vec2 p = this->getPositionAt(tileCoordinate); tile->setAnchorPoint(Vec2::ZERO); tile->setPosition(p); tile->setPositionZ((float)getVertexZForPos(tileCoordinate)); tile->setOpacity(this->getOpacity()); tile->setTag(index); this->addChild(tile, index); _spriteContainer.insert(std::pair<int, std::pair<Sprite*, int> >(index, std::pair<Sprite*, int>(tile, gid))); // tile is converted to sprite. setFlaggedTileGIDByIndex(index, 0); } } return tile; }
// used only when parsing the map. useless after the map was parsed // since lot's of assumptions are no longer true Sprite * TMXLayer::appendTileForGID(uint32_t gid, const Vec2& pos) { if (gid != 0 && (static_cast<int>((gid & kTMXFlippedMask)) - _tileSet->_firstGid) >= 0) { Rect rect = _tileSet->getRectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); // Z could be just an integer that gets incremented each time it is called. // but that wouldn't work on layers with empty tiles. // and it is IMPORTANT that Z returns an unique and bigger number than the previous one. // since _atlasIndexArray must be ordered because `bsearch` is used to find the GID for // a given Z. (github issue #16512) intptr_t z = getZForPos(pos); Sprite *tile = reusedTileWithRect(rect); setupTileSprite(tile ,pos ,gid); // optimization: // The difference between appendTileForGID and insertTileforGID is that append is faster, since // it appends the tile at the end of the texture atlas ssize_t indexForZ = _atlasIndexArray->num; // don't add it using the "standard" way. insertQuadFromSprite(tile, indexForZ); // append should be after addQuadFromSprite since it modifies the quantity values ccCArrayInsertValueAtIndex(_atlasIndexArray, (void*)z, indexForZ); // Validation for issue #16512 CCASSERT(_atlasIndexArray->num == 1 || _atlasIndexArray->arr[_atlasIndexArray->num-1] > _atlasIndexArray->arr[_atlasIndexArray->num-2], "Invalid z for _atlasIndexArray"); return tile; } return nullptr; }
// TMXLayer - adding helper methods Sprite * TMXLayer::insertTileForGID(unsigned int gid, const Point& pos) { Rect rect = m_pTileSet->rectForGID(gid); rect = CC_RECT_PIXELS_TO_POINTS(rect); KDintptr z = (KDintptr)(pos.x + pos.y * m_tLayerSize.width); Sprite *tile = reusedTileWithRect(rect); setupTileSprite(tile, pos, gid); // get atlas index unsigned int indexForZ = atlasIndexForNewZ(z); // Optimization: add the quad without adding a child this->insertQuadFromSprite(tile, indexForZ); // insert it into the local atlasindex array ccCArrayInsertValueAtIndex(m_pAtlasIndexArray, (void*)z, indexForZ); // update possible children if (m_pChildren && m_pChildren->count()>0) { Object* pObject = nullptr; CCARRAY_FOREACH(m_pChildren, pObject) { Sprite* child = static_cast<Sprite*>(pObject); if (child) { unsigned int ai = child->getAtlasIndex(); if ( ai >= indexForZ ) { child->setAtlasIndex(ai+1); } } }
Rect NinePatchImageParser::parseCapInset() const { Rect capInsets; Vec2 horizontalLine = this->parseHorizontalMargin(); Vec2 verticalLine = this->parseVerticalMargin(); if(_isRotated) { capInsets = Rect(verticalLine.y, _imageFrame.size.height - horizontalLine.y, verticalLine.y - verticalLine.x, horizontalLine.y - horizontalLine.x); } else { capInsets = Rect(horizontalLine.x, verticalLine.x, horizontalLine.y - horizontalLine.x, verticalLine.y - verticalLine.x); } capInsets = CC_RECT_PIXELS_TO_POINTS(capInsets); return capInsets; }
CCRect CC3Billboard::getBoundingBoxInPixels( CCNode* pNode ) { return CC_RECT_PIXELS_TO_POINTS( pNode->boundingBox() ); }
void SpriteFrame::setCenterRectInPixels(const Rect& centerRect) { _centerRect = CC_RECT_PIXELS_TO_POINTS(centerRect); }
void SpriteFrame::setRectInPixels(const Rect& rectInPixels) { _rectInPixels = rectInPixels; _rect = CC_RECT_PIXELS_TO_POINTS(rectInPixels); }
CCRect CCNode::boundingBox() { CCRect ret = boundingBoxInPixels(); return CC_RECT_PIXELS_TO_POINTS(ret); }
Sprite* Factory::createMonster() { Sprite* mons = Sprite::create("Monster.png", CC_RECT_PIXELS_TO_POINTS(Rect(364,0,42,42))); monster.pushBack(mons); return mons; }
void CCSpriteFrame::setRectInPixels(const CCRect& rectInPixels) { m_obRectInPixels = rectInPixels; m_obRect = CC_RECT_PIXELS_TO_POINTS(rectInPixels); }
// on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Scene::init() ) { return false; } visibleSize = Director::getInstance()->getVisibleSize(); origin = Director::getInstance()->getVisibleOrigin(); en = false; TMXTiledMap* tmx = TMXTiledMap::create("map.tmx"); tmx->setPosition(visibleSize.width / 2, visibleSize.height / 2); tmx->setAnchorPoint(Vec2(0.5, 0.5)); tmx->setScale(Director::getInstance()->getContentScaleFactor()); this->addChild(tmx, 0); //创建一张贴图 auto texture = Director::getInstance()->getTextureCache()->addImage("$lucia_2.png"); //从贴图中以像素单位切割,创建关键帧 auto frame0 = SpriteFrame::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(0, 0, 113, 113))); //使用第一帧创建精灵 player = Sprite::createWithSpriteFrame(frame0); player->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)); addChild(player, 3); //hp条 Sprite* sp0 = Sprite::create("hp.png", CC_RECT_PIXELS_TO_POINTS(Rect(0, 320, 420, 47))); Sprite* sp = Sprite::create("hp.png", CC_RECT_PIXELS_TO_POINTS(Rect(610, 362, 4, 16))); //使用hp条设置progressBar hp = 100; pT = ProgressTimer::create(sp); pT->setScaleX(90); pT->setAnchorPoint(Vec2(0, 0)); pT->setType(ProgressTimerType::BAR); pT->setBarChangeRate(Point(1, 0)); pT->setMidpoint(Point(0, 1)); pT->setPercentage(hp); pT->setPosition(Vec2(origin.x + 14 * pT->getContentSize().width, origin.y + visibleSize.height - 2 * pT->getContentSize().height)); addChild(pT, 1); sp0->setAnchorPoint(Vec2(0, 0)); sp0->setPosition(Vec2(origin.x + pT->getContentSize().width, origin.y + visibleSize.height - sp0->getContentSize().height)); addChild(sp0, 0); // 静态动画 idle.reserve(1); idle.pushBack(frame0); // 攻击动画 attack.reserve(17); for (int i = 0; i < 17; i++) { auto frame = SpriteFrame::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(113 * i, 0, 113, 113))); attack.pushBack(frame); } // 可以仿照攻击动画 // 死亡动画(帧数:22帧,高:90,宽:79) auto texture2 = Director::getInstance()->getTextureCache()->addImage("$lucia_dead.png"); // Todo dead.reserve(22); for (int i = 0; i < 22; ++i) { auto frame= SpriteFrame::createWithTexture(texture2, CC_RECT_PIXELS_TO_POINTS(Rect(79 * i, 0, 79, 90))); dead.pushBack(frame); } // 运动动画(帧数:8帧,高:101,宽:68) auto texture3 = Director::getInstance()->getTextureCache()->addImage("$lucia_forward.png"); // Todo run.reserve(8); for (int i = 0; i < 8; ++i) { auto frame = SpriteFrame::createWithTexture(texture3, CC_RECT_PIXELS_TO_POINTS(Rect(68 * i, 0, 68, 101))); run.pushBack(frame); } auto w = Label::createWithTTF("W", "fonts/arial.ttf", 36); auto wItem = MenuItemLabel::create(w, CC_CALLBACK_1(HelloWorld::moveWCallback,this)); wItem->setPosition(100, 100); auto s = Label::createWithTTF("S", "fonts/arial.ttf", 36); auto sItem = MenuItemLabel::create(s, CC_CALLBACK_1(HelloWorld::moveSCallback, this)); sItem->setPosition(100, 50); auto a = Label::createWithTTF("A", "fonts/arial.ttf", 36); auto aItem = MenuItemLabel::create(a, CC_CALLBACK_1(HelloWorld::moveACallback, this)); aItem->setPosition(50, 50); auto d = Label::createWithTTF("D", "fonts/arial.ttf", 36); auto dItem = MenuItemLabel::create(d, CC_CALLBACK_1(HelloWorld::moveDCallback, this)); dItem->setPosition(150, 50); auto x = Label::createWithTTF("X", "fonts/arial.ttf", 36); auto xItem = MenuItemLabel::create(x, CC_CALLBACK_1(HelloWorld::moveXCallback, this)); xItem->setPosition(visibleSize.width-50, 100); auto y = Label::createWithTTF("Y", "fonts/arial.ttf", 36); auto yItem = MenuItemLabel::create(y, CC_CALLBACK_1(HelloWorld::moveYCallback, this)); yItem->setPosition(visibleSize.width-100, 50); auto menu = Menu::create(wItem, sItem, aItem, dItem, xItem, yItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); dtime = 160; auto temp = CCString::createWithFormat("%d", dtime); time = Label::createWithTTF(temp->getCString(),"fonts/arial.ttf",36); time->setPosition(visibleSize.width / 2, visibleSize.height - 100); this->addChild(time,1); schedule(schedule_selector(HelloWorld::timeCallback), 1.0f, kRepeatForever, 1.0f); schedule(schedule_selector(HelloWorld::moveCallback), 3.0f, kRepeatForever, 1.0f); sc = 0; UserDefault::getInstance()->setIntegerForKey("score", sc); auto temp1 = CCString::createWithFormat("%d", sc); score = Label::createWithTTF(temp1->getCString(), "fonts/arial.ttf", 36); score->setPosition(visibleSize.width / 2, visibleSize.height - 50); this->addChild(score, 1); auto fac = Factory::getInstance(); for (int i = 0; i < 10; ++i) { Sprite* m = fac->createMonster(); float x1 = random(origin.x, visibleSize.width); float y1 = random(origin.y, visibleSize.height); m->setPosition(x1, y1); this->addChild(m, 3); } return true; }
Sprite* SubTest::createSpriteWithTag(int tag) { TextureCache *cache = Director::getInstance()->getTextureCache(); Sprite* sprite = NULL; switch (subtestNumber) { /// case 1: case 2: { sprite = Sprite::create("Images/grossinis_sister1.png"); _parentNode->addChild(sprite, 0, tag+100); break; } case 3: case 4: { Texture2D *texture = cache->addImage("Images/grossinis_sister1.png"); sprite = Sprite::createWithTexture(texture, Rect(0, 0, 52, 139)); _parentNode->addChild(sprite, 0, tag+100); break; } /// case 5: { int idx = (CCRANDOM_0_1() * 1400 / 100) + 1; char str[32] = {0}; sprintf(str, "Images/grossini_dance_%02d.png", idx); sprite = Sprite::create(str); _parentNode->addChild(sprite, 0, tag+100); break; } case 6: case 7: case 8: { int y,x; int r = (CCRANDOM_0_1() * 1400 / 100); y = r / 5; x = r % 5; x *= 85; y *= 121; Texture2D *texture = cache->addImage("Images/grossini_dance_atlas.png"); sprite = Sprite::createWithTexture(texture, Rect(x,y,85,121)); _parentNode->addChild(sprite, 0, tag+100); break; } /// case 9: { int y,x; int r = (CCRANDOM_0_1() * 6400 / 100); y = r / 8; x = r % 8; char str[40] = {0}; sprintf(str, "Images/sprites_test/sprite-%d-%d.png", x, y); sprite = Sprite::create(str); _parentNode->addChild(sprite, 0, tag+100); break; } case 10: case 11: case 12: { int y,x; int r = (CCRANDOM_0_1() * 6400 / 100); y = r / 8; x = r % 8; x *= 32; y *= 32; Texture2D *texture = cache->addImage("Images/spritesheet1.png"); sprite = Sprite::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(x,y,32,32))); _parentNode->addChild(sprite, 0, tag+100); break; } /// case 13: { int test = (CCRANDOM_0_1() * 3); if(test==0) { // Switch case 1 sprite = Sprite::create("Images/grossinis_sister1.png"); _parentNode->addChild(sprite, 0, tag+100); } else if(test==1) { // Switch case 6 int y,x; int r = (CCRANDOM_0_1() * 1400 / 100); y = r / 5; x = r % 5; x *= 85; y *= 121; Texture2D *texture = cache->addImage("Images/grossini_dance_atlas.png"); sprite = Sprite::createWithTexture(texture, Rect(x,y,85,121)); _parentNode->addChild(sprite, 0, tag+100); } else if(test==2) { int y,x; int r = (CCRANDOM_0_1() * 6400 / 100); y = r / 8; x = r % 8; x *= 32; y *= 32; Texture2D *texture = cache->addImage("Images/spritesheet1.png"); sprite = Sprite::createWithTexture(texture, CC_RECT_PIXELS_TO_POINTS(Rect(x,y,32,32))); _parentNode->addChild(sprite, 0, tag+100); } } default: break; } return sprite; }