0

動き回るアニメーションのスプライトを作りたいです。そのため、キャラクターのすべての動きにスプライト シートを使用し、さまざまなアニメーション ステージをアニメーション化します。

Firefox を搭載した PC ではすべて正常に動作しますが、Chrome と iPhone ではアニメーションが部分的に壊れています。ここで PC http://fiddle.jshell.net/WnjB6/48/でテストできます。

iPhoneの場合は、ここに直接アクセスしてください: http://fiddle.jshell.net/WnjB6/48/show/

iPhone のみでアニメーション中にスプライトシートの 2 列目が表示されないことがわかりました。小さいスプライトシートでは、これは機能しています。こちらをご覧ください

PC http://jsfiddle.net/WnjB6/7/

iphone http://jsfiddle.net/WnjB6/7/show

iPhone のファイル サイズや Chrome での画像の処理方法に制限はありますか?

ここにソースがあります

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

// Constructor for an animation object
// image: graphics source
// x, y: position to draw the animation
// width, height: size of each tile
// htiles: number of tiles horizontally in the image source
var Animation = function(image, x, y, width, height, htiles) {
    this.image = image;
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.htiles = htiles;
    this.animations = {};
    this.currentAnimation = [0];
    this.currentFrame = 0;
}

// Add animation to the object
Animation.prototype.add = function(name, frames) {
    this.animations[name] = frames;
};

// Select animation by name
Animation.prototype.play = function(name) {
    this.currentAnimation = this.animations[name];
    this.currentFrame = 0;
};

// Advance animation, and draw the tile
Animation.prototype.nextFrame = function() {
    // Move to next frame in current animation
    this.currentFrame = (this.currentFrame + 1) % this.currentAnimation.length;
    // Extract which image tile that is
    var tile = this.currentAnimation[this.currentFrame];
    this.drawTile(tile);
};

// Draw the given tile at the current position
Animation.prototype.drawTile = function(tile) {
    // Clear region we are going to draw on
    context.clearRect(this.x, this.y, this.width, this.height);
    context.drawImage(this.image, (tile % this.htiles) * this.width, Math.floor(tile / this.htiles) * this.height, this.width, this.height, this.x, this.y, this.width, this.height);
};

// Initialize the animation
var image = new Image();
image.src = 'http://www.playa.cc/pic/playerstant.png';

var player1 = new Animation(image, 0, 0, 51, 51, 2);
var aniStates = ['stand', 'right', 'walk'];
player1.add(aniStates[0], [65, 67, 69, 71, 73, 75, 77, 79]);
player1.add(aniStates[1], [0, 2, 4, 6, 8, 10, 12, 14, 16]);
player1.add(aniStates[2], [72, 74, 76, 78, 1, 3, 5, 7, 9]);


// Start with the walking animation, and start animating
player1.play(aniStates[0]);
var interval = setInterval(function() {
    player1.nextFrame();
}, 200);

// Toggle animation between standing and walking every 3 seconds
var mode = 0;
setInterval(function() {
    player1.play(aniStates[mode++]);
    mode = mode % aniStates.length;
}, 3000);

ありがとうございます

4

1 に答える 1

1

長い質問に短い答えを出すには:

クリッピングが元の画像から部分的に外れている場合、iPhone の Chrome と Safari はキャンバスに画像を表示しません。たとえば、画像の寸法が x:100、y:100 で、次のように描画するとします。

context.drawImage(image, 0, 0, 101 /* エラー */, 100, 0, 0, 100, 100)

drawImage仕様も参照

于 2013-01-01T23:29:32.063 に答える