이제 마지막으로 이미지를 옴 기는 코딩을 설명합니다.
필요한 코딩만 표시하고, 설명은 진하게 표시하였습니다.
완전히 이해가 됐으면 좋겠네요.
좀 시간을 들여서라도 전체를 이해하기 바랍니다.
var correctPuzzlePieces:Number = 0;
var holder:MovieClip;
init();
function init(){
내용 생략
초기화 즉 초기화면 생성
}
function onLoadImg(evt:Event):void{
내용 생략
for (var i:Number = 0; i < 8; i++)
{
puzzlePieceBmp = new BitmapData(widthPuzzlePiece, heightPuzzlePiece);
puzzlePieceBmp.copyPixels(puzzleBmp,
new Rectangle(x,y,widthPuzzlePiece,heightPuzzlePiece), new Point(0,0));
puzzlePieceBmp는 8개로 나누어진 이미지
makePuzzlePiece(puzzlePieceBmp, i);
아래의 makePuzzlePiece 함수에
8번 puzzlePieceBmp와 i를 가지고 가서 실행한다.
i는 for 문에 의해 0부터 7까지 8번 갑니다.
내용 생략
}
내용 생략
}
function makePuzzlePiece(puzzlePiece:BitmapData, index:int){
var puzzlePieceClip:Bitmap = new Bitmap(puzzlePiece);
var tmp2:MovieClip = new MovieClip();
tmp2.addChild(puzzlePieceClip);
tmp2.name = String(index) // Added for Strict Mode
tmp2.name에 문자인 스트링 0부터 7까지를 넣는다.
holder.addChild(tmp2);
앞 강좌에서 설명한대로 puzzlePieceClip에 puzzlePiece 넣고,
tmp2에 puzzlePieceClip 넣고,
holder에 tmp2를 넣는다.
holder.addEventListener("mouseDown", pieceMove);
holder.addEventListener("mouseUp", pieceMove);
앞 강좌 참조
puzzlePiecesArr.push(tmp2);
tmp2 8개를 puzzlePiecesArr 배열에 넣는다.
// This is used to check if the same piece has been placed
puzzlePiecesFound.push(tmp2.name);
tmp2.name 8개를 puzzlePiecesFound 배열에 넣는다.
앞으로 8개의 어떤 것을 클릭했는가를 구분하기 위해.
}
function pieceMove(evt:Event):void{
if(evt.type == "mouseDown"){
evt.target.startDrag();
} else if(evt.type == "mouseUp"){
evt.target.stopDrag();
앞 강좌 참조
var puzzlePieceIndex:Number = evt.target.name;
puzzlePieceIndex에 현재 클릭한 이름(0 ~ 7)을 넣는다.
// ADDED VV 4.3. Check if droppped inside of the grid
if(evt.target.dropTarget){
var puzzleBoardSpaceIndex:Number = evt.target.dropTarget.name;
}
드래그하는 아래의 표시 객체 또는 드래그해 놓은 표시 객체라면.
var puzzleBoardSpaceIndex에 evt.target.dropTarget.name를 넣는다.
이미지가 제자리에 왔으면
if(puzzlePieceIndex == puzzleBoardSpaceIndex)
{
var coordinate:Point = new Point(evt.target.dropTarget.x,
evt.target.dropTarget.y);
var coordinateGlobal:Point = new Point();
coordinateGlobal = puzzleBoardClip.localToGlobal(coordinate);
evt.target.x = coordinateGlobal.x;
evt.target.y = coordinateGlobal.y;
그 자리에 있도록 한다.
if(puzzlePiecesFound.length != 0)
{
for(var i:int = 0;i < puzzlePiecesFound.length; i++)
{
if(puzzlePiecesFound[i] == puzzlePieceIndex)
{
puzzlePiecesFound[i] = "Correct";
correctPuzzlePieces++;
correctPuzzlePieces 변수는 0으로 초기했고,
7이 되면 완료이겠죠^^
}
}
}
if(correctPuzzlePieces == totalPuzzlePieces)
{
puzzleSolved();
}
완료이면 puzzleSolved() 함수를 실행한다.
}
}
}
function puzzleSolved():void{
holder.visible = false;
var tmp:Bitmap = new Bitmap(puzzleBmp);
puzzleBoardClip.addChild(tmp);
다 제자리를 찾았으면, holder는 보이지 않고,
아래와 코딩과 같이 0.05초 마다 그 아래의 puzTrash() 메서드를 실행한다.
var timer:Timer = new Timer(50);
timer.start();
timer.addEventListener("timer", puzTrash);
}
function puzTrash(evt:Event):void{
if(threshold > 0xFFFFFF)
{
threshold = 0xFFFFFF;
evt.target.stop();
init();
앞에 있는 init() 함수를 실행하여, 초기화면으로 간다.
}
puzzleBmp.threshold(puzzleBmp, new Rectangle(0,0, puzzleBmp.width, puzzleBmp.height),
new Point(0,0), "<=", 0xFF000000 | threshold);
앞 강좌 참조
0xFF000000 | threshold은 0xFF000000 or threshold이다.
threshold *= 1.2;
threshold > 0xFFFFFF이 될 때까지 1.2씩 곱한다.
이 메서드는 자연스럽게 초기화면으로 가게 한다.
}


최근등록 댓글