MovieClipのフリッピング(鏡面反射)の不良マトリックス
-
05-07-2019 - |
質問
現在、AS3で素敵な小さな2Dゲームエンジンを開発しています。さまざまなアニメーションのXMLを介して、さまざまなFlashムービークリップを読み込むこともできます。機能の1つでは、特定のアクションを実行するときにアニメーションを反転させることもできます。そうすれば、(通常のように)左右両方に1つのアニメーションを作成できます。マトリックス変換が正しく機能しています。しかし、物事を前後に切り替えるための私の論理は間違っていると思います。その結果、間違ったアニメーションが間違った時間に再生されます。物理要素が関係しているため、キーを押すのではなく、移動するオブジェクトの速度に基づいてアニメーションを再生することにしました。
現在の状態のゲームへのリンクは次のとおりです。 http://parrisstudios.com/Game_Design/ gameEngineDemo /
問題は主に、アニメーションが間違った時間に再生されるか、または必要なときに再生されないことです。
ここに反転用の私のコードがあります(皆さんの考えを教えてください): 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);
}
}
解決
問題のコードはここにあると思います。
//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();
問題は、ここでダブルフリップをしていることです。これは、実際にクリップを変更から変更している場合に機能します。たとえば、右から右へ走る場合。右から左に走るとき、それはうまくいかない。それらはフリップされた同じクリップなので、この状況では2回フリップされます。これにより、ムーンウォーク動作が発生します:-)。
このロジックをクリーンアップし、フリップをリセットする方法を提供することをお勧めします。クリップの状態を保証できるように、元の向きに戻すのではなく、元の向きにリセットします。