Adding multiple instances of the same object to the stage - actionscript-3

I'm making a "spit" card game and I'm trying to be able to have 3 cards on the stage at once to be able to drag to the pile. So my question is how do I have multiple instances of the same object on the stage, but be able to assign each card different frames, etc. Here is the code to understand the context of it.
"cardArray" has 53 frames, 52 of which have a card face, 53rd is a "deck" picture.
//variables, constants
var cardDeck:Array = new Array();
var timer:Timer = new Timer(2000);
var j:int = 0;
var card:MovieClip = new CardArray();
var cardInDeck:Boolean;
const deckSize:int = 52;
//events:
card.addEventListener(MouseEvent.MOUSE_DOWN,fDown);
card.addEventListener(MouseEvent.MOUSE_UP,fUp);
startRandomise();
//This is where the unshuffled "deck" is created - values loaded into the array.
function createDeck():void
{
var i:int;
for(i =0; i<deckSize; i++)
{
cardDeck[i] = i+1;
}
}
function randomizeDeck( a : *, b : * ):int
{
return (Math.random()>.5) ? 1 : -1;
}
//Grab the value from the (presumably) current frame of the card
function convertCard(cardNo:int):int
{
var deckPos:int;
if (cardNo <= deckSize/4)
{
deckPos = cardNo;
}
else if (cardNo > deckSize/4 && cardNo <= deckSize/2)
{
deckPos = cardNo -13;
}
else if (cardNo > deckSize/2 && cardNo <=deckSize*3/4)
{
deckPos = cardNo -deckSize/2;
}
else if (cardNo >39)
{
deckPos = cardNo -39;
}
return deckPos;
}
btn.addEventListener(MouseEvent.MOUSE_UP,showArray);
function showArray(event:MouseEvent):void
{
if(j<deckSize /2)
{
addChild(card);
card.gotoAndStop(cardDeck[j]);
trace(cardDeck[j]+","+deckCompare[j]);
j++;
}
if(cardInDeck)
{
card.x = 200;
card.y = 200;
cardInDeck+false;
}
}
function fDown(evt:MouseEvent)
{
card.startDrag();
}
function fUp(evt:MouseEvent)
{
stopDrag();
if(card.hitTestObject(deck))
{
trace("deck: "+convertCard(deck.currentFrame));
trace("card: "+convertCard(card.currentFrame));
if(convertCard(card.currentFrame) == convertCard(deck.currentFrame)+1 || convertCard(card.currentFrame) == convertCard(deck.currentFrame)-1 || convertCard(card.currentFrame) == 13 && convertCard(deck.currentFrame) == 1 || convertCard(card.currentFrame) == 1 && convertCard(deck.currentFrame) == 13 || convertCard(deck.currentFrame) == 14)
{
deck.gotoAndStop(card.currentFrame);
removeChild(card);
cardInDeck=true;
}
else
{
card.x = 200;
card.y = 200;
}
}
else
{
card.x = 200;
card.y = 200;
}
}
function startRandomise():void
{
createDeck();
cardDeck.sort(randomizeDeck);
cardDeck.sort(randomizeDeck);
trace("random: "+cardDeck);
trace("deckCompare: "+deckCompare);
card.x = 200;
card.y = 200;
deck.gotoAndStop(53);
}

To have multiple cards, you need to instantiate more than one card. I don't know anything about the card game spit and don't understand the purpose of many of your functions, but you'll want to do something along these lines:
Create a function that generates (instantiates) a new card:
function createCard(faceFrame):MovieClip {
var tmpCard:MovieClip = new CardArray(); //this creates a new instance of a card
tmpCard.addEventListener(MouseEvent.MOUSE_DOWN, cardMouseDown); //make the card call the mouse down function when that event is triggered
tmpCard.gotoAndStop(faceFrame); //assign the card it's face (frame)
addChild(tmpCard); //add the card to the screen
return tmpCard;
}
Then, in your mouse down handler (I renamed it to be more clear what is is), you can do the following to card that was mouse downed:
function cardMouseDown(evt:MouseEvent) {
//the events currentTarget property is reference to the item that you attached the listener to, so in this case the card who triggered the mouse down
card = evt.currentTarget as MovieClip; //assign the current clicked card to the global card variable (so we can access it in the mouse up function)
card.startDrag();
//add the mouse up listener on the stage, since there are cases where you can drag so fast that the mouse isn't over the item it's dragging (and then if you were to release the mouse at that moment it wouldn't dispatch the event)
stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUp);
}
Then your mouse up function would look something like this:
function stageMouseUp(evt:MouseEvent) {
//remove the mouse up listener from the stage
stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUp);
card.stopDrag();
if(card.hitTestObject(deck))
{
......//rest if the code
Seems like you would want to generate a new card in the showArray function you have? So something like this:
function showArray(event:MouseEvent):void
{
var newCard:MovieClip = createCard(cardDeck[j]);
j++
newCard.y = newCard.x = 200;
trace(cardDeck[j]+","+deckCompare[j]);
}
As an aside, you should consider calling your Card object something more sensible, like Card instead of CardArray, as it's not an array...

Related

Add moveclips on stage when elements of an array are clicked in as3

I have an array with six buttons called "sostaletters" and an array with six elements called "addedletters". What I want is every time a button is been clicked a new movieclip from the array "addedletters" to be added on stage. For example if the third element of the array "sostaletters" is been clicked then the third element of the array "addedletters" to be added on stage. How i can do that?
This is what i have done for my array "sostaletters"
var sostaletters:Array = [a7,a17,a24,a1,a18]
for each (var letter:MovieClip in sostaletters) {
letter.buttonMode = true;
letter.isClicked = false;
letter.addEventListener(MouseEvent.CLICK, kanoklick2);
function kanoklick2(event:MouseEvent):void
{
event.target.alpha = 0.5;
if(event.currentTarget.isClicked == false){
clickCount2 ++;
event.currentTarget.isClicked = true;
sostaletters[i].x = positionsArray[i].xPos;
sostaletters[i].y = positionsArray[i].yPos;
setChildIndex(MovieClip(e.currentTarget), numChildren - 1);
}
if(clickCount2 == sostaletters.length){
addChild(welldoneman);
myTimer.start();
if (contains(kremmala)) {
removeChild(kremmala)
}
for (var i:int= 0; i< wrongletters.length; i++)
{
wrongletters[i].removeEventListener(MouseEvent.CLICK, kanoklick);
}
for (var o:int= 0; o< sostaletters.length; o++)
{
sostaletters[o].removeEventListener(MouseEvent.CLICK, kanoklick2);
}
trace("All buttons have been clicked");
}
}
}
Neal Davis has already gave a right answer. But I shall make a code review for more clarity. See my comments in the code.
var sostaletters:Array = [a7,a17,a24,a1,a18]
// "sostaletters" is an Array. So it is better to use iteration by index
for each (var letter:MovieClip in sostaletters) {
letter.buttonMode = true;
letter.isClicked = false;
letter.addEventListener(MouseEvent.CLICK, kanoklick2);
// You will create "kanoklick2" function at each iteration.
// Move this declaration from this loop
function kanoklick2(event:MouseEvent):void {
event.target.alpha = 0.5;
// There is no need to compare a boolean property with "false" or "true"
// You can write "if(event.currentTarget.isClicked)"
if(event.currentTarget.isClicked == false) {
clickCount2 ++;
event.currentTarget.isClicked = true;
// You declare an "i" variable below.
// So the compiler will not fire en error.
// But you will not get an index of the current letter.
sostaletters[i].x = positionsArray[i].xPos;
sostaletters[i].y = positionsArray[i].yPos;
setChildIndex(MovieClip(e.currentTarget), numChildren - 1);
}
// If number of "sostaletters" are not changed
// you can save its value in a variable for an example "sostalettersNum"
if (clickCount2 == sostaletters.length) {
addChild(welldoneman);
myTimer.start();
if (contains(kremmala)) {
removeChild(kremmala)
}
// You declare an "i" variable. And use it above to get a current letter.
// But an "i" will not contain its index.
// It will be equal to "wrongletters.length"
for (var i:int= 0; i

how to call actionscript functions from flash timeline

I am using AS3, and i want to call function 'startMatchThree' which is in a MatchThree.as (external actionscript file) into the flahs main timeline actions.
here is my code:
package {
import flash.display.;
import flash.events.;
import flash.text.*;
import flash.utils.Timer;
public class MatchThree extends MovieClip {
// constants
static const numPieces:uint = 4;
static const spacing:Number = 45;
static const offsetX:Number = 30;
static const offsetY:Number = 50;
// game grid and mode
private var grid:Array;
private var gameSprite:Sprite;
private var firstPiece:Piece;
private var isDropping,isSwapping:Boolean;
private var gameScore:int;
// set up grid and start game
public function startMatchThree() {
// create grid array
grid = new Array();
for(var gridrows:int=0;gridrows<8;gridrows++) {
grid.push(new Array());
}
setUpGrid();
isDropping = false;
isSwapping = false;
gameScore = 0;
addEventListener(Event.ENTER_FRAME,movePieces);
}
public function setUpGrid() {
// loop until valid starting grid
while (true) {
// create sprite
gameSprite = new Sprite();
// add 64 random pieces
for(var col:int=0;col<8;col++) {
for(var row:int=0;row<8;row++) {
addPiece(col,row);
}
}
// try again if matches are present
if (lookForMatches().length != 0) continue;
// try again if no possible moves
if (lookForPossibles() == false) continue;
// no matches, but possibles exist: good board found
break;
}
// add sprite
addChild(gameSprite);
}
// create a random piece, add to sprite and grid
public function addPiece(col,row:int):Piece {
var newPiece:Piece = new Piece();
newPiece.x = col*spacing+offsetX;
newPiece.y = row*spacing+offsetY;
newPiece.col = col;
newPiece.row = row;
newPiece.type = Math.ceil(Math.random()*4);
newPiece.gotoAndStop(newPiece.type);
newPiece.select.visible = false;
gameSprite.addChild(newPiece);
grid[col][row] = newPiece;
newPiece.addEventListener(MouseEvent.CLICK,clickPiece);
return newPiece;
}
// player clicks on a piece
public function clickPiece(event:MouseEvent) {
var piece:Piece = Piece(event.currentTarget);
// first one selected
if (firstPiece == null) {
piece.select.visible = true;
firstPiece = piece;
// clicked on first piece again
} else if (firstPiece == piece) {
piece.select.visible = false;
firstPiece = null;
// clicked on second piece
} else {
firstPiece.select.visible = false;
// same row, one column over
if ((firstPiece.row == piece.row) && (Math.abs(firstPiece.col-piece.col) == 1)) {
makeSwap(firstPiece,piece);
firstPiece = null;
// same column, one row over
} else if ((firstPiece.col == piece.col) && (Math.abs(firstPiece.row-piece.row) == 1)) {
makeSwap(firstPiece,piece);
firstPiece = null;
// bad move, reassign first piece
} else {
firstPiece = piece;
firstPiece.select.visible = true;
}
}
}
// start animated swap of two pieces
public function makeSwap(piece1,piece2:Piece) {
swapPieces(piece1,piece2);
// check to see if move was fruitful
if (lookForMatches().length == 0) {
swapPieces(piece1,piece2);
} else {
isSwapping = true;
}
}
// swap two pieces
public function swapPieces(piece1,piece2:Piece) {
// swap row and col values
var tempCol:uint = piece1.col;
var tempRow:uint = piece1.row;
piece1.col = piece2.col;
piece1.row = piece2.row;
piece2.col = tempCol;
piece2.row = tempRow;
// swap grid positions
grid[piece1.col][piece1.row] = piece1;
grid[piece2.col][piece2.row] = piece2;
}
// if any pieces are out of place, move them a step closer to being in place
// happens when pieces are swapped, or they are dropping
public function movePieces(event:Event) {
var madeMove:Boolean = false;
for(var row:int=0;row<8;row++) {
for(var col:int=0;col<8;col++) {
if (grid[col][row] != null) {
// needs to move down
if (grid[col][row].y < grid[col][row].row*spacing+offsetY) {
grid[col][row].y += 5;
madeMove = true;
// needs to move up
} else if (grid[col][row].y > grid[col][row].row*spacing+offsetY) {
grid[col][row].y -= 5;
madeMove = true;
// needs to move right
} else if (grid[col][row].x < grid[col][row].col*spacing+offsetX) {
grid[col][row].x += 5;
madeMove = true;
// needs to move left
} else if (grid[col][row].x > grid[col][row].col*spacing+offsetX) {
grid[col][row].x -= 5;
madeMove = true;
}
}
}
}
// if all dropping is done
if (isDropping && !madeMove) {
isDropping = false;
findAndRemoveMatches();
// if all swapping is done
} else if (isSwapping && !madeMove) {
isSwapping = false;
findAndRemoveMatches();
}
}
// gets matches and removes them, applies points
public function findAndRemoveMatches() {
// get list of matches
var matches:Array = lookForMatches();
for(var i:int=0;i<matches.length;i++) {
var numPoints:Number = (matches[i].length-1)*50;
for(var j:int=0;j<matches[i].length;j++) {
if (gameSprite.contains(matches[i][j])) {
var pb = new PointBurst(this,numPoints,matches[i][j].x,matches[i][j].y);
addScore(numPoints);
gameSprite.removeChild(matches[i][j]);
grid[matches[i][j].col][matches[i][j].row] = null;
affectAbove(matches[i][j]);
}
}
}
// add any new piece to top of board
addNewPieces();
// no matches found, maybe the game is over?
if (matches.length == 0) {
if (!lookForPossibles()) {
endGame();
}
}
}
when I put 'startMatchThree' in the actions tab in timeline, it is showing this error message:
1180: Call to a possibly undefined method startMatchThree.
SO how to solve this error!!
Thank you for the help!! :)
startMatchThree() is a method of the MatchThree class, so you need a reference to an instance of MatchThree. Is there a symbol on the timeline somewhere which is linked to the MatchThree class? Then you can do something like this, where "matchThree" is the instance name of the symbol:
matchThree.startMatchThree();
Are you instantiating the MatchThree class in code? Then you just need to use that code reference, something like this,:
var matchThree:MatchThree = new MatchThree();
matchThree.startMatchThree();

AS3 Object Bin and Reset Button

I am trying to create a game for kids where they can drag letters on to a stage to make words.
I want to add a 'trash can' where users can drag letters they no longer need to dispose of them. I have created the movie clip but am totally unsure how to make it function using AS3.
I would also like to add a reset button so that the stage reverts to it's original state. Again, I have drawn it up and added the little as3 that I am aware of (to make it a button) but if anyone could assist with how to actually make this happen, I would be grateful.
The files are here: SWF | FLA and the code for the game is as follows:
import flash.display.MovieClip;
for (var i=1; i<27; i++)
{
this["object" + i].addEventListener(MouseEvent.MOUSE_DOWN, onStart);
this["object" + i].addEventListener(MouseEvent.MOUSE_UP, onStop);
}
var sx = 0,sy = 0;
function onStart(e)
{
sx = e.currentTarget.x;
sy = e.currentTarget.y;
e.currentTarget.startDrag();
}
function onStop(e)
{
if ( e.target.dropTarget != null &&
e.target.dropTarget.parent == dest &&
e.currentTarget.name != "copy" )
{
var objectClass:Class =
getDefinitionByName(getQualifiedClassName(e.currentTarget)) as Class;
var copy:MovieClip = new objectClass();
copy.name = "copy";
this.addChild(copy);
copy.x = e.currentTarget.x;
copy.y = e.currentTarget.y;
e.currentTarget.x = sx;
e.currentTarget.y = sy;
copy.addEventListener(MouseEvent.MOUSE_DOWN, onStart);
copy.addEventListener(MouseEvent.MOUSE_UP, onStop);
}
e.currentTarget.stopDrag();
}
resetButton.addEventListener(MouseEvent.CLICK, reset);
resetButton.buttonMode = true;
function reset(event:MouseEvent):void
{
//Not sure what AS3 to add here to reset to original state
}
I have already gave you the solution here Flash AS3 Clone, Drag and Drop
Here, I am providing a detail solution on how to drag objects inside a bin and remove them.
For dropping copied objects inside a bin, after dragging is stopped, check collision with bin object. for more info see,
copiedObject.hitTestObject(binObject)
For e.g.
First create trash-can MovieClip on the stage and give it an instance name 'trashCan' and add following lines to your onStop()(below e.currentTarget.stopDrag();)function like so:
UPDATE:
var copiedObjsArr:Array = [];
function onStop(e)
{
if ( e.target.dropTarget != null &&
e.target.dropTarget.parent == dest &&
e.currentTarget.name != "copy" )
{
//Code here remains same
//.......
//Keep collecting copied letters for further access in `reset()` function
copiedObjsArr.push(copy);
}
else if(e.currentTarget.name == "copy") //this is 'else if' (newly added)
{
var tarObject:MovieClip = e.currentTarget;
// These detects collision of dragged object with the trashCan
if(tarObject.hitTestObject(trashCan)) {
//These removes dragged object from the display list (not from the memory)
removeChild(tarObject);
tarObject = null; //to garbage
}
}
e.currentTarget.stopDrag();
}
And your reset() becomes like so:
function reset(event:MouseEvent):void
{
if(copiedObjsArr.length > 0)
{
//Traverse through all copied letters
for(var i:int = 0; i<copiedObjsArr.length; i++)
{
var objToRemove:MovieClip = copiedObjsArr[i];
removeChild(objToRemove);
objToRemove = null;
}
//Finally empty the array
copiedObjsArr = [];
}
}

swap depthes when movie clips loaded from library

I have two movie clips in the library with linkage.
on the stage I have two buttons - each load a movie clip to a specific mc target on the stage.
I also have a third button that removes the mc target, to clear the stage.
I want to know how can I change the code in AS3 so the loaded movie clips will not show at the same time, but swap each other, like I used to use depth in AS2.
This is the code:
var myIgool = new igool ();
var myRibooa = new ribooa ();
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
mc_all.addChild (myIgool);
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
mc_all.addChild (myRibooa);
}
unloadall.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_6);
function fl_MouseClickHandler_6(event:MouseEvent):void
{
removeChild(mc_all);
;
}
I would recommend something like this:
var myIgool = new igool ();
var myRibooa = new ribooa ();
mc_all.addChild(myIgool);
mc_all.addChild(myRibooa);
myIgool.visible = false;
myRibooa.visible = false;
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
myIgool.visible = true;
myRibooa.visible = false;
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
myIgool.visible = false;
myRibooa.visible = true;
}
unloadall.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_6);
function fl_MouseClickHandler_6(event:MouseEvent):void
{
myIgool.visible = false;
myRibooa.visible = false;
}
But if you really want to swap, you could also do the following, however I recommend setting the visible flag as it's more efficient rather than covering something up that wouldn't need to draw.
loadigool.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);
function fl_MouseClickHandler_3(event:MouseEvent):void
{
if (myIgool.parent != mc_all)
{
mc_all.addChild(myIgool);
}
else
{
mc_all.setChildIndex(myIgool, mc_all.numChildren - 1);
}
}
loadribooa.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);
function fl_MouseClickHandler_4(event:MouseEvent):void
{
if (myRibooa.parent != mc_all)
{
mc_all.addChild(myRibooa);
}
else
{
mc_all.setChildIndex(myRibooa, mc_all.numChildren - 1);
}
}

Starling mouseover detection

I'm making a point & click game where is clickable objects. When player moves mouse over the object, there appear a tooltip beside the cursor. It works almost as intended with the code below:
private function added():void
{
removeEventListener(Event.ADDED, added);
this.addEventListener(TouchEvent.TOUCH, onTouch);
}
protected function onTouch(e:TouchEvent):void
{
var touchHover:Touch = e.getTouch(this, TouchPhase.HOVER);
if (touchHover)
{
trace("show");
//mouse is hovered over this object. Therefore call Hovertext:
if (Game.hoverText.message != name_)
Game.hoverText.message = name_
}
else
{
//mouse leaves the object
trace("hide");
Game.hoverText.hideMessage(name_);
}
}
However, it has a strange problem I don't understand. If I move mouse over object and then move it downwards still staying over the object, it triggers hiding function in every second frame or so. Same thing happens when I move cursor to the right, but not when moving it up or left.
So my question is what is wrong with my code? Is this even the best way to go to detect when mouse rolls over object and when it rolls away?
EDIT: I have been going through following iterations with each of them the same problem:
var touch:Touch = event.getTouch(this);
if (touch == null) {
// Set Object alpha to 0;
//trace("pois");
Game.hoverText.hideMessage(name_);
}
else if (touch.phase == TouchPhase.HOVER) {
// Set Object alpha to 1;
//trace("paalla");
if (Game.hoverText.message != name_)
Game.hoverText.message = name_;
}
else {
// for a phase BEGIN/MOVE/STATIONARY case
// see if the touch is over the bounds of the tile (assuming 'this' is the tile)
HELPER_POINT.x = touch.globalX;
HELPER_POINT.y = touch.globalY;
this.globalToLocal(HELPER_POINT, HELPER_POINT);
if(this.hitTest(HELPER_POINT, true) != null)
{
// Set Object alpha to 1; over tile
trace("paalla");
}
else
{
// Set Object alpha to 0; not over tile
trace("pois");
}
}
var touchHover:Touch = e.getTouch(this);
if (touchHover && touchHover.phase == TouchPhase.HOVER)
{
trace("show");
//mouse is hovered over this object. Therefore call Hovertext:
if (Game.hoverText.message != name_)
Game.hoverText.message = name_
}
if (touchHover == null)
{
//mouse leaves the object
trace("hide");
Game.hoverText.hideMessage(name_);
}
Also here is swf for demonstating the problem:
http://www.students.tut.fi/~salmi26/ScorpionBox.html
private function isPressed(event:TouchEvent):void
{
var touch:touch = event.getTouch(this);
if(touch.phase == TouchPhase.BEGAN){
trace("show");
//mouse is hovered over this object. Therefore call Hovertext:
if (Game.hoverText.message != name_)
Game.hoverText.message = name_
} else if(touch.phase == TouchPhase.ENDED){
trace("release");
//stop doing stuff
removeEventListener(Event.ENTER_FRAME, onButtonHold);
}
}
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
private var touches:Vector.<Touch>;
private var touch:Touch;
private var touchStage:Touch;
private function onTouch(e:TouchEvent=null):void {
touches= e.getTouches(DisplayObject(e.target));
if (touches.length == 1)
{
touch = touches[0];
if (touch.phase == TouchPhase.BEGAN){
m_TouchTarget = touch.target;
//HERE IS A MOUSE DOWN
}
if (touch.phase == TouchPhase.ENDED){
m_TouchEndedPoint = new Point(touch.globalX, touch.globalY);
if (stage.hitTest(m_TouchEndedPoint, true) == m_TouchTarget)
{
//HERE IS A MOUSE UP
}
}
if (touch.phase == TouchPhase.MOVED){
m_TouchEndedPoint = new Point(touch.globalX, touch.globalY);
//HERE IS A MOUSE OUT
if (stage.hitTest(m_TouchEndedPoint, true) != m_TouchTarget)
{
//HERE IS A MOUSE OVER
}
}
}
}