开发者

Rendering of graphics different depending on position

When drawing parallel vertical lines with a fixed distance between them (1.75 pixels) with a non-integer x-value-offset to both lines, the lines are drawn differently based on the offset. In the picture below are two pairs of very close together vertical lines. As you can see, they look very different. This is frustrating, especially when animating the sprite.

alt text http://img94.imageshack.us/img94/8606/lines2.png

Any ideas how ensure that sprites-with-non-integer-positions' graphics will visually display the same?

package
{

import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;

public class tmpa extends Sprite
{

private var _sp1:Sprite;
private var _sp2:Sprite;
private var _num:Number;

public function tmpa( ):void
{
    stage.align = StageAlign.TOP_LEFT;
    stage.scaleMode = StageScaleMode.NO_SCALE;

    _sp1 = new Sprite( );
    drawButt( _sp1, 0 );
   开发者_StackOverflow中文版 _sp1.x = 100;
    _sp1.y = 100;

    _num = 0;
    _sp2 = new Sprite( );
    drawButt( _sp2, _num );
    _sp2.x = 100;
    _sp2.y = 200;

    addChild( _sp1 );
    addChild( _sp2 );

    addEventListener( Event.ENTER_FRAME, efCb, false, 0, true );
}

private function efCb( evt:Event ):void
{   _num += .1;
    if (_num > 400)
    {   _num = 0;
    }
    drawButt( _sp2, _num );
}

private function drawButt( sp:Sprite, offset:Number ):void
{
    var px1:Number = 1 + offset;
    var px2:Number = 2.75 + offset;

    sp.graphics.clear( );
    sp.graphics.lineStyle( 1, 0, 1, true );
    sp.graphics.moveTo( px1, 1 );
    sp.graphics.lineTo( px1, 100 );

    sp.graphics.lineStyle( 1, 0, 1, true );
    sp.graphics.moveTo( px2, 1 );
    sp.graphics.lineTo( px2, 100 );
}

}
}

edit from original post which thought the problem was tied to the x-position of the sprite.


although flash internally uses twips, when rendering it can still only render to full pixels. There is no way (that I know of) to fix this, other than casting to an int before setting the .x position. You would then have to have a Number that keeps the actual floating point position, and cast it to an int before setting .x

Edit:

private function drawButt( sp:Sprite, offset:Number ):void
{
    var px1:int = 1 + offset;
    var px2:Number = px1 + 1.75;

    sp.graphics.clear( );
    sp.graphics.lineStyle( 1, 0, 1, true );
    sp.graphics.moveTo( px1, 1 );
    sp.graphics.lineTo( px1, 100 );

    sp.graphics.lineStyle( 1, 0, 1, true );
    sp.graphics.moveTo( px2, 1 );
    sp.graphics.lineTo( px2, 100 );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜