2013-08-06 71 views
1

我想知道如何将一个精灵从屏幕中心移动到距屏幕上边缘10个像素。因此使它看起来像是在向上移动。科科斯HTML 5,动画雪碧

这是到目前为止我的代码

var LoginLayer = cc.Layer.extend({ 

init:function() { 

    ////////////////////////////// 
    // 1. super init first 
    this._super(); 

    ///////////////////////////// 
    // 2. add a menu item with "X" image, which is clicked to quit the program 
    // you may modify it. 
    // ask director the window size 
    var size = cc.Director.getInstance().getWinSize(); 

    //Create Background Layer 
    var background = cc.LayerColor.create(g_Colors.cedarWoodFinish, size.width, size.height); 
    background.setAnchorPoint(cc.p(0.5,0.5)); 

    //Create Logo Layer 
    var logo = cc.Sprite.create(s_logo); 
    logo.setAnchorPoint(cc.p(0.5, 1)); 
    logo.setPosition(cc.p(size.width/2, size.height + ((logo.getContentSize().height * logo.getScaleY()) /2))); 
    logo.setScale(0.5); 

    //Add Layers To Scene 
    this.addChild(background); 
    this.addChild(logo); 

    var logoMoveUpAnimation = cc.MoveTo.create(2, cc.p(size.width/2, size.height - 10)); 
    logo.runAction(logoMoveUpAnimation); 
} 
}); 

var LoginScene = cc.Scene.extend({ 
onEnter:function() { 
    this._super(); 
    var layer = new LoginLayer(); 
    this.addChild(layer); 
    layer.init(); 
} 
}); 

回答

1

你使用的是什么版本科科斯2D-X的?对于Cocos 2d-x rc0 2.1.3,您可以使用这些代码行来移动Sprite。

CCSize size = CCDirector::sharedDirector()->getWinSize(); 
actualY = size.height - 10; 
CCFiniteTimeAction* actionMove = 
     CCMoveTo::create(9.0,ccp(logo->getContentSize().width/2, actualY)); 
     CCFiniteTimeAction* actionMoveDone = 
     CCCallFuncN::create(this, 
     callfuncN_selector(HelloWorld::LogoMoveFinished)); 
    logo->runAction(CCSequence::create(actionMove, 
     actionMoveDone, NULL)); 

此处徽标移动到位置(logosize/2,screenheight-10pixels)。当徽标已被移动时LogoMoveFinished函数被调用。

+0

我使用Ccos 2D Html v2.1.4 –

+0

我没有使用2.1.4,但你可以在rc0中试试这个2.1.3我会工作。 – lazyandroid

+0

好的,谢谢:) –