用于翻转(镜面反射)MovieClip 的错误矩阵
我目前正在使用 AS3 开发一个不错的小型 2D 游戏引擎。您甚至可以通过 XML 加载不同的 Flash 影片剪辑以实现不同的动画。其中一项功能还允许您在执行特定操作时翻转动画。这样你就可以有 1 个左右动画(像往常一样)。我的矩阵变换工作正常;然而,我认为我来回翻转事情的逻辑是不正确的。结果是在错误的时间播放错误的动画。我决定根据移动物体的速度而不是按键来播放动画,因为我涉及到物理元素。
以下是游戏当前状态的链接:http://parrisstudios.com/Game_Design/gameEngineDemo/< /a>
问题主要是动画在不正确的时间播放,或者在应该播放的时候没有播放。
这是我的翻转代码(让我知道你们的想法): ps:方向变量是角色最后面对的方向(本例中为左或右,稍后在代码中描述)
private function drawJumpMode():void {
var change:Boolean = false;
//decide whether to swap graphics or continue playing
if (Math.abs(particleGroup.particles[0].velocity.y) < epsilon && Math.abs(particleGroup.particles[0].velocity.x) < epsilon && direction == "right") {
if (lastClipAction != "stillRight"){
lastClipAction = "stillRight";
change = true;
}
}
else if (Math.abs(particleGroup.particles[0].velocity.y) < epsilon && Math.abs(particleGroup.particles[0].velocity.x) < epsilon && direction == "left") {
if (lastClipAction != "stillLeft"){
lastClipAction = "stillLeft";
change = true;
}
}
else if (particleGroup.particles[0].velocity.y > 0 && Math.abs(particleGroup.particles[0].velocity.x) < epsilon) {
if (lastClipAction != "down"){
lastClipAction = "down";
change = true;
}
}
else if (particleGroup.particles[0].velocity.y < 0 && Math.abs(particleGroup.particles[0].velocity.x) < epsilon) {
if (lastClipAction != "up"){
lastClipAction = "up";
change = true;
}
}
else if (particleGroup.particles[0].velocity.x > 0) {
if (lastClipAction != "right") {
lastClipAction = "right";
direction = "right";
change = true;
}
}
else if (particleGroup.particles[0].velocity.x < 0) {
if (lastClipAction != "left"){
lastClipAction = "left";
direction = "left";
change = true;
}
}
//If the movie clip has changed, swap the old one back into the all clips library and get the new one.
if (change) {
//set flip to false whenever there is a change in movie clip
flipped = false;
//if the movie clip was flipped before, this should flip it
//back to normal
flip();
//remove movie clip from the scene
game.removeChild(playingMovieClip);
//check it back into the movie clip library
allClips.addChild(playingMovieClip);
//add the movie clip into the scene
playingMovieClip = MovieClip(allClips.getChildByName(actionToClip[lastClipAction + "Name"]));
game.addChild(playingMovieClip);
}
//Flip if needed
flip();
//set it to true so that it won't be constantly flipped.
flipped = true;
//set movie clip to be center over the main particle
playingMovieClip.x = particleGroup.particles[0].center.x;
playingMovieClip.y = particleGroup.particles[0].center.y;
}
private function flip():void {
//If a transformation is required, then do so
if (actionToClip[lastClipAction + "H"]=="1" && !flipped){
flipHorizontal(playingMovieClip);
}
if (actionToClip[lastClipAction + "V"]=="1" && !flipped){
flipVertical(playingMovieClip);
}
}
I am currently working on a nice little 2D game engine in AS3. You can even load in different flash movie clips through XML for the different animations. One of the features also allows you to flip an animation when you perform a certain action. That way you could have 1 animation for both left and right (as usual). I have the matrix transformation working correctly; however, I think my logic for flipping things back and forth is incorrect. The result is that the wrong animation will play at the wrong time. I decided to make the animation played based on the velocity of the moving object rather than on key press since I have physics elements involved.
Here is a link to the game in its current state: http://parrisstudios.com/Game_Design/gameEngineDemo/
The issue is mainly that animations play at incorrect times, or don't play when they should.
Here is my code for the flipping (let me know what you guys think):
ps: the direction variable is the last direction the character is facing (left or right in this case, it is described later in the code)
private function drawJumpMode():void {
var change:Boolean = false;
//decide whether to swap graphics or continue playing
if (Math.abs(particleGroup.particles[0].velocity.y) < epsilon && Math.abs(particleGroup.particles[0].velocity.x) < epsilon && direction == "right") {
if (lastClipAction != "stillRight"){
lastClipAction = "stillRight";
change = true;
}
}
else if (Math.abs(particleGroup.particles[0].velocity.y) < epsilon && Math.abs(particleGroup.particles[0].velocity.x) < epsilon && direction == "left") {
if (lastClipAction != "stillLeft"){
lastClipAction = "stillLeft";
change = true;
}
}
else if (particleGroup.particles[0].velocity.y > 0 && Math.abs(particleGroup.particles[0].velocity.x) < epsilon) {
if (lastClipAction != "down"){
lastClipAction = "down";
change = true;
}
}
else if (particleGroup.particles[0].velocity.y < 0 && Math.abs(particleGroup.particles[0].velocity.x) < epsilon) {
if (lastClipAction != "up"){
lastClipAction = "up";
change = true;
}
}
else if (particleGroup.particles[0].velocity.x > 0) {
if (lastClipAction != "right") {
lastClipAction = "right";
direction = "right";
change = true;
}
}
else if (particleGroup.particles[0].velocity.x < 0) {
if (lastClipAction != "left"){
lastClipAction = "left";
direction = "left";
change = true;
}
}
//If the movie clip has changed, swap the old one back into the all clips library and get the new one.
if (change) {
//set flip to false whenever there is a change in movie clip
flipped = false;
//if the movie clip was flipped before, this should flip it
//back to normal
flip();
//remove movie clip from the scene
game.removeChild(playingMovieClip);
//check it back into the movie clip library
allClips.addChild(playingMovieClip);
//add the movie clip into the scene
playingMovieClip = MovieClip(allClips.getChildByName(actionToClip[lastClipAction + "Name"]));
game.addChild(playingMovieClip);
}
//Flip if needed
flip();
//set it to true so that it won't be constantly flipped.
flipped = true;
//set movie clip to be center over the main particle
playingMovieClip.x = particleGroup.particles[0].center.x;
playingMovieClip.y = particleGroup.particles[0].center.y;
}
private function flip():void {
//If a transformation is required, then do so
if (actionToClip[lastClipAction + "H"]=="1" && !flipped){
flipHorizontal(playingMovieClip);
}
if (actionToClip[lastClipAction + "V"]=="1" && !flipped){
flipVertical(playingMovieClip);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信有问题的代码就在这里。
问题是你在这里做了两次翻转。如果您实际上正在更改更改中的剪辑,则此功能可以找到。例如,如果您从右侧跑步变为右侧站立。当你从右跑转到左跑时就会出错。由于它们是刚刚翻转的同一个剪辑,因此在这种情况下您最终会翻转两次。这会导致月球漫步行为:-)。
我建议清理这个逻辑并提供一种重置翻转的方法。不要翻转回原始方向,而是重置回原来的方向,这样您就可以保证剪辑的状态。
I believe the code in question is here.
The problem is that you are doing a double flip here. This works find if you are in fact changing clips from the change. For example if you go from running right to standing right. It goes wrong when you go from running right to running left. Since they are the same clip just flipped you end up doing two flips in this situation. This causes the moonwalk behaviour :-).
I would suggest cleaning up this logic and providing a way to reset the flipping. Instead of flipping back to the original orientation, reset back to it so you can guarantee the state of the clip.