Cocos2d-android (03) 向量
向量的基本运算及动作
1 import org.cocos2d.actions.interval.CCJumpBy; 2 import org.cocos2d.actions.interval.CCMoveBy; 3 import org.cocos2d.layers.CCLayer; 4 import org.cocos2d.nodes.CCSprite; 5 import org.cocos2d.types.CGPoint; 6 7 public class GameLayer extends CCLayer { 8 9 private CCSprite spriteA; 10 private CCSprite spriteB; 11 12 public GameLayer() { 13 spriteA = CCSprite.sprite("player.png"); 14 spriteB = CCSprite.sprite("player.png"); 15 16 this.addChild(spriteA); 17 this.addChild(spriteB); 18 19 CGPoint startPoint = CGPoint.ccp(200, 200); 20 CGPoint deltaPoint = CGPoint.ccp(400, 400); 21 22 spriteA.setPosition(startPoint); 23 spriteB.setPosition(startPoint); 24 25 //向量加法 26 CGPoint addPoint = CGPoint.ccpAdd(startPoint, deltaPoint); 27 //向量减法 28 CGPoint subPoint = CGPoint.ccpSub(startPoint, deltaPoint); 29 //向量的乘法(一个向量乘以一个浮点数) 30 CGPoint multPoint = CGPoint.ccpMult(startPoint, 2.0f); 31 //计算单位向量 32 CGPoint normalizePoint = CGPoint.ccpNormalize(startPoint); 33 34 spriteB.setPosition(addPoint); 35 36 //五秒钟内基于当前位置移动deltaPoint(x增加400,y增加400) 37 CCMoveBy moveBy = CCMoveBy.action(5, deltaPoint); 38 //五秒钟内四次从当前位置跳跃deltaPoint(x增加400,y增加400) 39 CCJumpBy jumpBy = CCJumpBy.action(5, deltaPoint, 200, 4); 40 41 spriteB.runAction(jumpBy); 42 } 43 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。