ActionScript3问题

时间:2016-03-31 01:48:08

标签: actionscript-3 flash

所以我在使用ActionScript时遇到了一些问题。我正在创建一个项目,您可以点击一分钱并将其拖到刮刮卡上,然后刮掉涂层并显示文本。

一切都由两层组成。底层是卡片的图像,上面有文字(我把它变成了位图)。顶层是一个带有实心填充的矩形(刮开涂层),我将其转换为属性名称为 maskee3 的影片剪辑。到目前为止我成功了。

当我遇到问题时,我尝试在舞台上添加另一张刮刮卡。我做了同样的过程,将卡片变成了位图,矩形刮掉了涂层(顶层)到一个属性名为 maskee4 的影片剪辑中。当我点击预览电影时,它仍然只是划掉了初始卡片。老实说,我不知道该怎么做,如何解决这个问题,让它划伤所有这些

以下是我所做的编码,此时我刚刚在黑暗中拍摄。任何帮助,将不胜感激。谢谢!

/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/

penny.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);

function fl_ClickToDrag(event:MouseEvent):void
{
penny.startDrag();
}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);

function fl_ReleaseToDrop(event:MouseEvent):void
{
penny.stopDrag();


}

//start code for scratch off//


/*************************
MASKING
**************************/

/*
Initial variables
*/
// Change lineSize to control size of your eraser
var lineSize:Number=4;
// doDraw is true when user's mouse is down
var doDraw:Boolean=false;
// resumeDrawing is true when user drags their mouse off stage while drawing
var resumeDrawing:Boolean=false;

/*
Create a bitmap to act as our image mask
Add it to stage & cache as bitmap
*/
var erasableBitmapData:BitmapData = new BitmapData(400, 400, true,                0xFFFFFFFF);
var erasableBitmap:Bitmap = new Bitmap(erasableBitmapData);
erasableBitmap.cacheAsBitmap = true;
addChild(erasableBitmap);

/*
Set the erasable bitmap as a mask of our image & cache image as bitmap
*/


maskee3.cacheAsBitmap = true;
maskee3.mask = erasableBitmap;

maskee4.cacheAsBitmap = true;
maskee4.mask = erasableBitmap;


/*************************
ERASER
**************************/

/*
Create a sprite for drawing the eraser lines onto
Set its lineStyle, and move the line to the current mouse x,y
*/
var eraserClip:Sprite = new Sprite();
initEraser();
function initEraser():void{
eraserClip.graphics.lineStyle(lineSize,0xff0000);
eraserClip.graphics.moveTo(stage.mouseX,stage.mouseY);

}

/*
Create a bitmap to copy the erased lines to.
This is required to ensure a smooth erase effect (applying eraserClip 
directly to erasableBitmapData leaves jaggy edges  around alpha areas. 
If anyone knows why, I'd love to know!)
*/
var drawnBitmapData:BitmapData = new BitmapData(400, 400, true, 0x00000000);
var drawnBitmap:Bitmap = new Bitmap(drawnBitmapData);

/*************************
MOUSE EVENTS
**************************/

/*
Add event listeners for mouse movements
*/
stage.addEventListener(MouseEvent.MOUSE_MOVE,maskMove);
stage.addEventListener(MouseEvent.ROLL_OUT, maskOut); 
stage.addEventListener(MouseEvent.ROLL_OVER,maskOver);
stage.addEventListener(MouseEvent.MOUSE_DOWN,startDrawing);
stage.addEventListener(MouseEvent.MOUSE_UP,stopDrawing);

/*
Mouse down handler
Begin drawing
*/
function startDrawing(e:MouseEvent):void {
eraserClip.graphics.moveTo(stage.mouseX,stage.mouseY);
doDraw=true;
}

/*
Mouse up handler
Stop drawing
*/
function stopDrawing(e:MouseEvent):void {
doDraw=false;
resumeDrawing = false;
}

/*
Mouse out handler
If user was drawing when they moved mouse off stage, we will need
to resume drawing when they move back onto stage.
*/
function maskOut(e:Event):void {
if (doDraw){
    resumeDrawing = true;
}
}

/*
Mouse over handler
If user's mouse if still down, continue drawing from the point where 
the mouse re-entered the stage.
*/
function maskOver(e:MouseEvent):void {
if (resumeDrawing){
    resumeDrawing = false;
    eraserClip.graphics.moveTo(stage.mouseX,stage.mouseY);
}
}

/*
Mouse move handler
*/
function maskMove(e:MouseEvent):void {
if (doDraw && !resumeDrawing){
    // Draw a line to current mouse position
    eraserClip.graphics.lineTo(stage.mouseX,stage.mouseY);
    // Clear the drawn bitmap by filling it with a transparent color
    drawnBitmapData.fillRect(drawnBitmapData.rect, 0x00000000); 
    // Copy our eraser drawing into the erasable bitmap
    // (This is required to ensure the smooth alpha edges on our eraser are       retained)
    drawnBitmapData.draw(eraserClip , new Matrix(), null, BlendMode.NORMAL);
    // Fill the erasable bitmap with a solid color
    erasableBitmapData.fillRect(erasableBitmapData.rect, 0xFFFFFFFF);
    // Copy the scribble bitmap to our main bitmap, with blendmode set to    ERASE
    // This erases the portion of the mask that has been drawn.
    erasableBitmapData.draw(drawnBitmap, new Matrix(), null, BlendMode.ERASE);
}
// Update after event to ensure no lag
e.updateAfterEvent();
}

/************************RESET BUTTON
**************************/

reset_btn.addEventListener(MouseEvent.CLICK,reset);

function reset(e:Event):void {
eraserClip.graphics.clear();
initEraser();
erasableBitmapData.fillRect(erasableBitmapData.rect, 0xFFFFFFFF);
}

1 个答案:

答案 0 :(得分:1)

一个" masker"单身" maskee"如果你愿意保持单一的掩饰,你可以合并maskee。

只需替换它。

maskee3.cacheAsBitmap = true;
maskee3.mask = erasableBitmap;

maskee4.cacheAsBitmap = true;
maskee4.mask = erasableBitmap;

由此:

var container:Sprite = new Sprite();
container.addChild(maskee3);
container.addChild(maskee4);
container.cacheAsBitmap=true;
container.mask = erasableBitmap;
addChild(container);