Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

ddlightw

Embaralhar objetos no array

Recommended Posts

Como eu posso trocar a posição de moviclips com array?

Eu sei mudar para posição original, mas não sei como trocar as posições, inverter.

 

Exemplo:

 

Esse código pega o valor inicial x e y e faz a animação, mas como eu troco a posição de um objeto com outro?

 

Tweener.addTween(lastClip, {x:lastposX.x, y:lastposY.y, time:0.2, transition:"easeOutBack"});

Compartilhar este post


Link para o post
Compartilhar em outros sites

Sugestão, bem melhor as classes de animação da greensock.

 

Encontra exemplos de demonstração no site.

 

 

Abs

Compartilhar este post


Link para o post
Compartilhar em outros sites

Conheço, mas a minha dúvida é na estrutura do código, como eu faço para armazenar cada posição de um movieclip e depois eu inverto as posições.

Exemplo:

 

quadrado_mc. x = this.x;// 400

quadrado_mc. x = this.y;//300

 

triangulo_mc. x = this.x;// 600

triangulo_mc. y = this.y;// 400

 

Nesse caso seria simplismente criar uma váriavel com a posição inicial de cada um e depois colocar que a posição do triangulo é igual a posição do quadrado, mas não sei fazer isso com array.

 

Eu áprendi que quando tem um array tem que repassar a variável, mas não entendi porque:

 

 

for (var i=0; i<qtdImagens;i++) {
//IMAGENS//////////////
thumb_img = new Loader();
//carrega a imagem miniatura da pasta thumb
thumb_img.load(new URLRequest
("fotoCategorias/thumb/"+imagens + ".jpg"));
//MOVIE CLIPS/////////////
thumb_mc = new MovieClip();
//posicionamento
//thumb_mc.x =
//posicão inicial + incremento * (largura + distancia)
thumb_mc.x = 50 + i * (80 + 5);
thumb_mc.y = 50;
//icone de mão
thumb_mc.buttonMode =true;
//repasse da variavel
thumb_mc.imagemDaVez = i;

//listener
thumb_mc.addEventListener
(MouseEvent.CLICK, escolheDetalhe);
//movie clip recebe a imagem
thumb_mc.addChild(thumb_img);
//exibe o movie clip na tela
addChild(thumb_mc);
}//end for

 

Na tentativa de criar um jogo de arrastar que aceitasse qualquer alvo,fiz uns testes, só queria que prendesse a posição do alvo, consegui quando fiz uma pequena modificação, acrescentei um array para o alvo, obrigatoriamente tem que fazer o loop?

 

Código original, achei que era só trocar hitArray[dropIndex], por hitArray, mas não funcionou:

 



//Array to hold the target instances, the drop instances,
//and the start positions of the drop instances.
var hitArray:Array = new Array(hitTarget1,hitTarget2,hitTarget3,hitTarget4);
var dropArray:Array = new Array(drop1,drop2,drop3,drop4);
var positionsArray:Array = new Array();


//This adds the mouse down and up listener to the drop instances
//and add the starting x and y positions of the drop instances
//into the array.
for (var i:int = 0; i < dropArray.length; i++) {
dropArray[i].buttonMode = true;
dropArray[i].addEventListener(MouseEvent.MOUSE_DOWN, mdown);
dropArray[i].addEventListener(MouseEvent.MOUSE_UP, mUp);

positionsArray.push({xPos:dropArray[i].x, yPos:dropArray[i].y});
}

//This drags the object that has been selected and moves it
//to the top of the display list. This means you can't drag
//this object underneath anything.
function mdown(e:MouseEvent):void {
e.currentTarget.startDrag();
setChildIndex(MovieClip(e.currentTarget), numChildren - 1);
}

//This stops the dragging of the selected object when the mouse is
//released. If the object is dropped on the corresponding target
//then it get set to the x and y position of the target. Otherwise
//it returns to the original position.
function mUp(e:MouseEvent):void {
var dropIndex:int = dropArray.indexOf(e.currentTarget);
var target:MovieClip = e.currentTarget as MovieClip;

target.stopDrag();

if (target.hitTestObject(hitArray[dropIndex])) {
target.x = hitArray[dropIndex].x;
target.y = hitArray[dropIndex].y;
}else{
target.x = positionsArray[dropIndex].xPos;
target.y = positionsArray[dropIndex].yPos;
}
}

 

 

 

 

Código com a mudança, assim o movieclip aceita ser arrastado para qualquer um dos alvos:

 

//Array to hold the target instances, the drop instances,
//and the start positions of the drop instances.
var hitArray:Array = new Array(hitTarget1,hitTarget2,hitTarget3,hitTarget4);
var dropArray:Array = new Array(drop1,drop2,drop3,drop4);
var positionsArray:Array = new Array();


//This adds the mouse down and up listener to the drop instances
//and add the starting x and y positions of the drop instances
//into the array.
for (var i:int = 0; i < dropArray.length; i++) {
dropArray[i].buttonMode = true;
dropArray[i].addEventListener(MouseEvent.MOUSE_DOWN, mdown);
dropArray[i].addEventListener(MouseEvent.MOUSE_UP, mUp);

positionsArray.push({xPos:dropArray[i].x, yPos:dropArray[i].y});
}

//This drags the object that has been selected and moves it
//to the top of the display list. This means you can't drag
//this object underneath anything.
function mdown(e:MouseEvent):void {
e.currentTarget.startDrag();
setChildIndex(MovieClip(e.currentTarget), numChildren - 1);
}

//This stops the dragging of the selected object when the mouse is
//released. If the object is dropped on the corresponding target
//then it get set to the x and y position of the target. Otherwise
//it returns to the original position.
function mUp(e:MouseEvent):void {
//var dropIndex:int = dropArray.indexOf(e.currentTarget);
var target:MovieClip = e.currentTarget as MovieClip;

target.stopDrag();

for(var i:int = 0; i < hitArray.length; i++) {
  if(target.hitTestObject(hitArray[i])) {
   target.x = hitArray[i].x;
   target.y = hitArray[i].y;
   break;
  }
 }
}





Compartilhar este post


Link para o post
Compartilhar em outros sites

Obrigado Elektra, conhecia a greensock, mas a minha dúvida é de como eu pego a posição dos movieclips e sorteio, sempre me confundo com array.

Não sei como funciona, devo criar um array de posição e depois onde posso e como devo colocar o código para mudar a posição?

Se eu tenho dois movieclips e sei que :

 

movie1.x movie1.y

movie2.x movie2.y

 

Eu tenho as posições dos movieclips no stage, que código eu poderia inverter as posições, o que eu preciso para trocar a posição dos movieclips. Eu poderia fazer:

 

 

movie1.x ==

movie2.x

 

Mas os clips ficariam na mesma posição.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.