AS3:限制函数的舞台空间?

发布于 2024-12-09 11:17:05 字数 2408 浏览 4 评论 0原文

我想知道如何防止用户在我的学校董事会之外绘图。

我的主板图像尺寸为 709.15 X 499.5。 所以我想到了这样的事情......

if(stage.stageWidth <= 709)

但是如果我的棋盘图像在舞台 boardActiva 上被称为变量,它应该更容易。

这是绘制的函数:

        private function dibujar(e:MouseEvent){         
        trace(e.localY);            
        tizaActiva.x = e.stageX;
        tizaActiva.y = e.stageY;
        if(dibujando){
        tabla.graphics.lineTo(e.stageX,e.stageY);
        }

这是完整的代码:

package  {  
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.*;
import flash.trace.Trace;
import flash.ui.Mouse;
import flash.display.Shape;
import fl.controls.ColorPicker;
import fl.motion.Color;
import fl.events.ColorPickerEvent;
public class pizarra extends MovieClip {        
private var colores:ColorPicker = new ColorPicker;      
private var boardActiva:board = new board;
private var tizaActiva:tiza = new tiza();
private var tabla:Shape = new Shape;
private var dibujando:Boolean;
    public function pizarra() {
        Mouse.hide();
        tabla.graphics.lineStyle(5,0xFFFFFF);
            // constructor code         
        boardActiva.x = 45;
        boardActiva.y = 40;
        addChild(boardActiva);
        addChild(tabla);
        addChild(colores);
        addChild(tizaActiva);
        dibujando = false;          
        stage.addEventListener(MouseEvent.MOUSE_DOWN, empezarDibujo);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, dibujar);
        stage.addEventListener(MouseEvent.MOUSE_UP, detenerDibujo);
        colores.addEventListener(ColorPickerEvent.CHANGE,cambiar);
    }

    private function empezarDibujo(e:MouseEvent):void{          
        trace(e.localY);
        tabla.graphics.moveTo(e.stageX,e.stageY);
        dibujando = true;           
    }       
    private function dibujar(e:MouseEvent){         
        trace(e.localY);

        tizaActiva.x = e.stageX;
        tizaActiva.y = e.stageY;
        if(dibujando){
        tabla.graphics.lineTo(e.stageX,e.stageY);
        }
    }       
    private function detenerDibujo(e:MouseEvent){           
        trace(e.localY);
        dibujando = false;

    }           
    private function cambiar(e:ColorPickerEvent){
            tabla.graphics.lineStyle(5,e.color);

        }       

}

}

I want to know how to prevent the user from drawing outside my school board.

My board image sizes 709.15 X 499.5.
So I thought of something like this...

if(stage.stageWidth <= 709)

But if my board image is being called as a variable on stage boardActiva it should be easier.

Here's the function that draws:

        private function dibujar(e:MouseEvent){         
        trace(e.localY);            
        tizaActiva.x = e.stageX;
        tizaActiva.y = e.stageY;
        if(dibujando){
        tabla.graphics.lineTo(e.stageX,e.stageY);
        }

And this is the full code:

package  {  
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.*;
import flash.trace.Trace;
import flash.ui.Mouse;
import flash.display.Shape;
import fl.controls.ColorPicker;
import fl.motion.Color;
import fl.events.ColorPickerEvent;
public class pizarra extends MovieClip {        
private var colores:ColorPicker = new ColorPicker;      
private var boardActiva:board = new board;
private var tizaActiva:tiza = new tiza();
private var tabla:Shape = new Shape;
private var dibujando:Boolean;
    public function pizarra() {
        Mouse.hide();
        tabla.graphics.lineStyle(5,0xFFFFFF);
            // constructor code         
        boardActiva.x = 45;
        boardActiva.y = 40;
        addChild(boardActiva);
        addChild(tabla);
        addChild(colores);
        addChild(tizaActiva);
        dibujando = false;          
        stage.addEventListener(MouseEvent.MOUSE_DOWN, empezarDibujo);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, dibujar);
        stage.addEventListener(MouseEvent.MOUSE_UP, detenerDibujo);
        colores.addEventListener(ColorPickerEvent.CHANGE,cambiar);
    }

    private function empezarDibujo(e:MouseEvent):void{          
        trace(e.localY);
        tabla.graphics.moveTo(e.stageX,e.stageY);
        dibujando = true;           
    }       
    private function dibujar(e:MouseEvent){         
        trace(e.localY);

        tizaActiva.x = e.stageX;
        tizaActiva.y = e.stageY;
        if(dibujando){
        tabla.graphics.lineTo(e.stageX,e.stageY);
        }
    }       
    private function detenerDibujo(e:MouseEvent){           
        trace(e.localY);
        dibujando = false;

    }           
    private function cambiar(e:ColorPickerEvent){
            tabla.graphics.lineStyle(5,e.color);

        }       

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕色琉璃 2024-12-16 11:17:05

如果我正确理解了你的问题,应该可以做到:

private function dibujar(e:MouseEvent)
{

    //trace(e.localY);

    tizaActiva.x = e.stageX;
    tizaActiva.y = e.stageY;

    if(dibujando && insideBoard(e.stageX,e.stageY) )
    {
        tabla.graphics.lineTo(e.stageX,e.stageY);

    }

}

  private function insideBoard(x:Number,y:Number):Boolean
  {
    return ( (x>= boardActiva.x) 
           && (x <= boardActiva.x + boardActiva.width )
           && (y >= boardActiva.y) 
           && (y <= boardActiva.y + boardActiva.height ) );
  }

希望能有所帮助,祝你的披萨好运;)

If I understood correctly your question, this should do it:

private function dibujar(e:MouseEvent)
{

    //trace(e.localY);

    tizaActiva.x = e.stageX;
    tizaActiva.y = e.stageY;

    if(dibujando && insideBoard(e.stageX,e.stageY) )
    {
        tabla.graphics.lineTo(e.stageX,e.stageY);

    }

}

  private function insideBoard(x:Number,y:Number):Boolean
  {
    return ( (x>= boardActiva.x) 
           && (x <= boardActiva.x + boardActiva.width )
           && (y >= boardActiva.y) 
           && (y <= boardActiva.y + boardActiva.height ) );
  }

Hope to be of some help, good luck with your pizarra ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文