Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 我正在为一个滑动拼图创建一个自动求解函数_Actionscript 3_Bitmap_Puzzle - Fatal编程技术网

Actionscript 3 我正在为一个滑动拼图创建一个自动求解函数

Actionscript 3 我正在为一个滑动拼图创建一个自动求解函数,actionscript-3,bitmap,puzzle,Actionscript 3,Bitmap,Puzzle,我是actionscript新手,我正在尝试一种“作弊”的方式来完成一个滑动拼图,当我点击[s]键时,它会调用一个求解拼图函数,该函数会生成一个for循环,将所有拼图块放在正确的位置。这个函数必须调用一个for循环,这些是我得到的指令。有人能帮我理解实现这一点所需的代码吗。我已经做了这个函数,当我按s键时它确实正确地调用了,但是过去的一切似乎并没有使代码实际移动。谢谢你给我的任何帮助 package { import flash.display.*; import flash.events.*;

我是actionscript新手,我正在尝试一种“作弊”的方式来完成一个滑动拼图,当我点击[s]键时,它会调用一个求解拼图函数,该函数会生成一个for循环,将所有拼图块放在正确的位置。这个函数必须调用一个for循环,这些是我得到的指令。有人能帮我理解实现这一点所需的代码吗。我已经做了这个函数,当我按s键时它确实正确地调用了,但是过去的一切似乎并没有使代码实际移动。谢谢你给我的任何帮助

package {
import flash.display.*;
import flash.events.*;
import flash.net.URLRequest;
import flash.geom.*;
import flash.utils.Timer;

public class SlidingPuzzle extends MovieClip {
    // space between pieces and offset
    static const pieceSpace:Number = 2;
    static const horizOffset:Number = 50;
    static const vertOffset:Number = 50;

    // number of pieces
    static const numPiecesHoriz:int = 5;
    static const numPiecesVert:int = 5;

    // random shuffle steps
    static const numShuffle:int = 200;

    // animation steps and time
    static const slideSteps:int = 10;
    static const slideTime:int = 250;

    // size of pieces
    private var pieceWidth:Number;
    private var pieceHeight:Number;

    // game pieces
    private var puzzleObjects:Array;

    // tracking moves
    private var blankPoint:Point;
    private var slidingPiece:Object;
    private var slideDirection:Point;
    private var slideAnimation:Timer;

    public function startSlidingPuzzle() {
        // blank spot is the bottom right
        blankPoint = new Point(numPiecesHoriz-1,numPiecesVert-1);

        // load the bitmap
        loadBitmap("oceanshore.jpg");

        // event to complete the puzzle for you.
        stage.addEventListener(KeyboardEvent.KEY_UP, solvePuzzle);

    }

    // get the bitmap from an external source
    public function loadBitmap(bitmapFile:String) {
        var loader:Loader = new Loader();
        loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingDone);
        var request:URLRequest = new URLRequest(bitmapFile);
        loader.load(request);
    }

    // bitmap done loading, cut into pieces
    public function loadingDone(event:Event):void {
        // create new image to hold loaded bitmap
        var image:Bitmap = Bitmap(event.target.loader.content);
        pieceWidth = image.width/numPiecesHoriz;
        pieceHeight = image.height/numPiecesVert;

        // cut into puzzle pieces
        makePuzzlePieces(image.bitmapData);

        // shuffle them
        shufflePuzzlePieces();
    }

    // cut bitmap into pieces
    public function makePuzzlePieces(bitmapData:BitmapData) {
        puzzleObjects = new Array();
        for(var x:uint=0;x<numPiecesHoriz;x++) {
            for (var y:uint=0;y<numPiecesVert;y++) {
                // skip blank spot
                if (blankPoint.equals(new Point(x,y))) continue;

                // create new puzzle piece bitmap and sprite
                var newPuzzlePieceBitmap:Bitmap = new Bitmap(new BitmapData(pieceWidth,pieceHeight));
                newPuzzlePieceBitmap.bitmapData.copyPixels(bitmapData,new Rectangle(x*pieceWidth,y*pieceHeight,pieceWidth,pieceHeight),new Point(0,0));
                var newPuzzlePiece:Sprite = new Sprite();
                newPuzzlePiece.addChild(newPuzzlePieceBitmap);
                addChild(newPuzzlePiece);

                // set location
                newPuzzlePiece.x = x*(pieceWidth+pieceSpace) + horizOffset;
                newPuzzlePiece.y = y*(pieceHeight+pieceSpace) + vertOffset;

                // create object to store in array
                var newPuzzleObject:Object = new Object();
                newPuzzleObject.currentLoc = new Point(x,y);
                newPuzzleObject.homeLoc = new Point(newPuzzlePiece.x,newPuzzlePiece.y);
                newPuzzleObject.piece = newPuzzlePiece;
                newPuzzlePiece.addEventListener(MouseEvent.CLICK,clickPuzzlePiece);
                puzzleObjects.push(newPuzzleObject);
            }
        }
    }

    //make the puzzle solve itself
    public function solvePuzzle(evt:KeyboardEvent)
    {
        if (evt.keyCode == 83)
        {   
            for(var i:int = 0; i<puzzleObjects.length; i++)
            {

            }
        }
    }


    // make a number of random moves
    public function shufflePuzzlePieces() {
        for(var i:int=0;i<numShuffle;i++) {
            shuffleRandom();
        }
    }

    // random move
    public function shuffleRandom() {
        // loop to find valid moves
        var validPuzzleObjects:Array = new Array();
        for(var i:uint=0;i<puzzleObjects.length;i++) {
            if (validMove(puzzleObjects[i]) != "none") {
                validPuzzleObjects.push(puzzleObjects[i]);
            }
        }
        // pick a random move
        var pick:uint = Math.floor(Math.random()*validPuzzleObjects.length);
        movePiece(validPuzzleObjects[pick],false);
    }

    public function validMove(puzzleObject:Object): String {
        // is the blank spot above
        if ((puzzleObject.currentLoc.x == blankPoint.x) &&
            (puzzleObject.currentLoc.y == blankPoint.y+1)) {
            return "up";
        }
        // is the blank spot below
        if ((puzzleObject.currentLoc.x == blankPoint.x) &&
            (puzzleObject.currentLoc.y == blankPoint.y-1)) {
            return "down";
        }
        // is the blank to the left
        if ((puzzleObject.currentLoc.y == blankPoint.y) &&
            (puzzleObject.currentLoc.x == blankPoint.x+1)) {
            return "left";
        }
        // is the blank to the right
        if ((puzzleObject.currentLoc.y == blankPoint.y) &&
            (puzzleObject.currentLoc.x == blankPoint.x-1)) {
            return "right";
        }
        // no valid moves
        return "none";
    }

    // puzzle piece clicked
    public function clickPuzzlePiece(event:MouseEvent) {
        // find piece clicked and move it
        for(var i:int=0;i<puzzleObjects.length;i++) {
            if (puzzleObjects[i].piece == event.currentTarget) {
                movePiece(puzzleObjects[i],true);
                break;
            }
        }
    }

    // move a piece into the blank space
    public function movePiece(puzzleObject:Object, slideEffect:Boolean) {
        // get direction of blank space
        switch (validMove(puzzleObject)) {
            case "up":
                movePieceInDirection(puzzleObject,0,-1,slideEffect);
                break;
            case "down":
                movePieceInDirection(puzzleObject,0,1,slideEffect);
                break;
            case "left":
                movePieceInDirection(puzzleObject,-1,0,slideEffect);
                break;
            case "right":
                movePieceInDirection(puzzleObject,1,0,slideEffect);
                break;
        }
    }

    // move the piece into the blank spot
    public function movePieceInDirection(puzzleObject:Object, dx,dy:int, slideEffect:Boolean) {
        puzzleObject.currentLoc.x += dx;
        puzzleObject.currentLoc.y += dy;
        blankPoint.x -= dx;
        blankPoint.y -= dy;

        // animate or not
        if (slideEffect) {
            // start animation
            startSlide(puzzleObject,dx*(pieceWidth+pieceSpace),dy*(pieceHeight+pieceSpace));
        } else {
            // no animation, just move
            puzzleObject.piece.x = puzzleObject.currentLoc.x*(pieceWidth+pieceSpace) + horizOffset;
            puzzleObject.piece.y = puzzleObject.currentLoc.y*(pieceHeight+pieceSpace) + vertOffset;
        }
    }

    // set up a slide
    public function startSlide(puzzleObject:Object, dx, dy:Number) {
        if (slideAnimation != null) slideDone(null);
        slidingPiece = puzzleObject;
        slideDirection = new Point(dx,dy);
        slideAnimation = new Timer(slideTime/slideSteps,slideSteps);
        slideAnimation.addEventListener(TimerEvent.TIMER,slidePiece);
        slideAnimation.addEventListener(TimerEvent.TIMER_COMPLETE,slideDone);
        slideAnimation.start();
    }

    // move one step in slide
    public function slidePiece(event:Event) {
        slidingPiece.piece.x += slideDirection.x/slideSteps;
        slidingPiece.piece.y += slideDirection.y/slideSteps;
    }

    // complete slide
    public function slideDone(event:Event) {
        slidingPiece.piece.x = slidingPiece.currentLoc.x*(pieceWidth+pieceSpace) + horizOffset;
        slidingPiece.piece.y = slidingPiece.currentLoc.y*(pieceHeight+pieceSpace) + vertOffset;
        slideAnimation.stop();
        slideAnimation = null;

        // check to see if puzzle is complete now
        if (puzzleComplete()) {
            clearPuzzle();
            gotoAndStop("gameover");
        }
    }

    // check to see if all pieces are in place
    public function puzzleComplete():Boolean {
        for(var i:int=0;i<puzzleObjects.length;i++) {
            if (!puzzleObjects[i].currentLoc.equals(puzzleObjects[i].homeLoc)) {
                return false;
            }
        }
        return true;
    }

    // remove all puzzle pieces
    public function clearPuzzle() {
        for (var i in puzzleObjects) {
            puzzleObjects[i].piece.removeEventListener(MouseEvent.CLICK,clickPuzzlePiece);
            removeChild(puzzleObjects[i].piece);
        }
        puzzleObjects = null;
    }

}
包{
导入flash.display.*;
导入flash.events.*;
导入flash.net.URLRequest;
导入flash.geom.*;
导入flash.utils.Timer;
公共类幻灯片拼图扩展MovieClip{
//工件间距和偏移量
静态常量片段空间:Number=2;
静态常数水平偏移:数字=50;
静态常数垂直偏移量:数字=50;
//件数
静态常量numPiecesHoriz:int=5;
静态常量numPiecesVert:int=5;
//随机洗牌步骤
静态常量numShuffle:int=200;
//动画步骤和时间
静态常数slideSteps:int=10;
静态常数slideTime:int=250;
//件数
私有变分片宽度:个数;
私有高度:数字;
//棋子
私有对象:数组;
//跟踪动作
私人var空白点:点;
私有var滑块:对象;
私有变量slideDirection:点;
私有变量slideAnimation:定时器;
公共函数启动LidingPuzzle(){
//空白点在右下角
空白点=新点(numPiecesHoriz-1,numPiecesVert-1);
//加载位图
loadBitmap(“oceanshore.jpg”);
//事件为您完成拼图。
stage.addEventListener(KeyboardEvent.KEY\u UP,solvePuzzle);
}
//从外部源获取位图
公共函数loadBitmap(位图文件:字符串){
变量加载器:加载器=新加载器();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadingDone);
var-request:URLRequest=新的URLRequest(位图文件);
加载(请求);
}
//位图完成加载,切成块
公共函数加载完成(事件:事件):无效{
//创建新图像以保存加载的位图
var image:Bitmap=Bitmap(event.target.loader.content);
pieceWidth=image.width/numPiecesHoriz;
pieceHeight=image.height/numPiecesVert;
//切成拼图
制作拼图块(image.bitmapData);
//洗牌
shufflePuzzlePieces();
}
//将位图切块
公共函数(bitmapData:bitmapData){
拼图对象=新数组();

对于(var x:uint=0;x首先,程序必须知道对象的放置位置,以便正确放置。最好的方法是记录工件的初始位置

在设置工件的新位置之前,创建对象并获取其初始位置

newPuzzleObject.initialLoc = new Point(newPuzzlePiece.x,newPuzzlePiece.y);
for(i:uint; i < puzzleObjects.length; i++)
{
    puzzleObjects[i].x = puzzleObjects[i].initialLoc.x;
    puzzleObjects[i].y = puzzleObjects[i].initialLoc.y;
}
对象创建必须在该部分之前,在工件的初始位置更改之前进行

// set location
newPuzzlePiece.x = x*(pieceWidth+pieceSpace) + horizOffset;
newPuzzlePiece.y = y*(pieceHeight+pieceSpace) + vertOffset;
在解决难题时,将它们全部放回初始位置

newPuzzleObject.initialLoc = new Point(newPuzzlePiece.x,newPuzzlePiece.y);
for(i:uint; i < puzzleObjects.length; i++)
{
    puzzleObjects[i].x = puzzleObjects[i].initialLoc.x;
    puzzleObjects[i].y = puzzleObjects[i].initialLoc.y;
}
(i:uint;i { 拼图对象[i].x=拼图对象[i].initialLoc.x; 拼图对象[i].y=拼图对象[i].initialLoc.y; }