I have a movie clip linkage named "trainglePoint" inside a movieclip with a "bgdemo" instance and I was wondering how will I get the "trianglePoint" to work.
also "gags" is the character that will get the "trianglePoint"
this is my code below, which doesn't work.
thanks!
edit: forgot to add that the 'trianglePoint' is put onto stage multiple times (dont know if that helps)
var pickUpsArray:Array = new Array();
stage.addEventListener (Event.ENTER_FRAME, pickUpItems);
public function pickUpItems (e:Event)
{
for (var i=0; i<numChildren;i++)
{
if (getChildAt(i) is bgdemo.trianglePoint)
{
pickUpsArray.push(getChildAt(i));
}
}
for (var j=0; j<pickUpsArray.length;j++)
{
if (gags.hitTestObject (pickUpsArray[j]))
{
removeChild(pickUpsArray[j]);
trace ("hitTestObject: YES");
}
}
}
You should instead use GetChildByName(name).
GetChildAt implies that you know where in the child layers the child you want is located.
So if your child is named "trianglePoint" and you want to access it, just use getChildByName("trianglePoint");
There are several problems with the rest of your code.
First, you push into an array but never remove from it. Using removechild does not remove the object from the array which means you will always test it for collision with "gags".
The other issue is that you are adding your object to the array in each frame. What you should do instead is get your object from GetChildByName, push it into the array THEN loop and interact with it.
I would do it like this:
import flash.display.MovieClip
public class bgdemo extends MovieClip
{
var pickUpsArray:Array
public function bgdemo()//constructor for your parent movieclip
{
pickUpsArray = new Array();
this.addEventListener (Event.ADDED_TO_STAGE, init_ok);
//this is to ensure the parent movieclip (bgdemo) is on the stage and we can access its children.
}
private function init_ok(e:Event):void
{
this.removeEventListener(Event.ADDED_TO_STAGE, init_ok);
//removing the listener so that we only do this once
pickUpsArray.push(this.getChildByName("trianglePoint"));
stage.addEventListener (Event.ENTER_FRAME, pickUpItems);
}
private function pickUpItems (e:Event):void
{
for (var j=0; j<pickUpsArray.length;j++)
{
if (gags.hitTestObject (pickUpsArray[j]))
{
removeChild(pickUpsArray[j]);
pickUpsArray.splice(j, 1); //removing the object from the array so we can't collide with it anymore
trace ("hitTestObject: YES");
}
}
}
}
You create a new Array in your bgdemo movieclip's constructor and add a listener to the fact that the movieclip is added to the stage. When the movieclip is added to the stage, (remove the "added to stage listener"), get your child, add it to the array and add a listener to enter frame.
On each frame you then test every object in your array (only 1 at this point) and "gags". If gags and the object collide, you then remove the object from both the display list and the array.
Try that and tell me if that works (the way you want it to work). If it's not what you wanted, then please be more precise on your original post ;)
Related
I have created an array of objects in my libGdx project like this:
Array<Bird> birds = new Array<Bird>();
Here Bird is one object class.
I want to draw 10 Bird objects one by one on the screen.So I did like this.
private void MakeBird() {
Bird b = objectFactory.createBird();
for (int i = 0; i < 10; i++) {
birds.add(b);
}
}
createBird method of objectFactory:
public Bird createBird() {
Bird bird = new Bird();
bird.setPosition(0,0);
return bird;
}
Here I want only 10 objects.But when I try to print the array size,it is incrementing continuousely.How can I implement this properly?
I also want to know how to draw these array objects using spriteBatch.
It will be very helpful if I get an explanation with code.
In order to keep the size of the birds array constant you need to modify your MakeBird() function into this
private void MakeBird() {
int birdsToAdd = 10 - birds.length;
for (int i = 0; i < birdsToAdd ; i++) {
birds.add(objectFactory.createBird());
}
}
Your version of MakeBird() would simply add the same object ten times into the birds array. So, why is this bad? Because Java objects are reference type, meaning that if you modify one object all the references to that object will also change. In your case, if you change the position of one bird in the array, all the birds position will change. I don't know if this is intentional. But if you want to add different Bird objects then you should use the MakeBird() function like I implemented above. Nice catch there by marius.
As for your second query about how to render these objects to the screen, what you can do is, update your Bird class to contain a render or a draw method (you can name the method whatever you want) and in your main class you should update your render method to call the render/draw method of the Bird class. You can do this as such
//Bird class
class Bird {
//Your data members
//Your member functions
public void draw(SpriteBatch batch) {
batch.draw(some_bird_image, bird_x_position, bird_y_position);
}
}
//Render method in main class
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
for(Bird bird : birds) {
bird.draw(batch);
}
batch.end();
}
You have to create a new Bird object every for-loop run. So write birds.add(objectFactory.createBird()) instead of birds.add(b). I think that should fix the first error.
You somehow need to implement the following like it is decribed here:
public class Game implements ApplicationAdapter {
private SpriteBatch batch;
private Texture texture;
public void create () {
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("example.png"));
}
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
batch.begin();
batch.draw(texture, 10, 10);
batch.end();
}
}
But I really recommend you to use Box2D, because it is very ease to use and you can find plenty of tutorials for it.
I'm making a game that have multiple instance of a MC in stage (a1,b1,c1....a2,b2,c2,d2,etc)
Every time to make a listener i have to write
Object(this).sopa.c1.btn.addEventListener(MouseEvent.CLICK, c1Click);
and create a function like this
function c1Click(event:MouseEvent):void
{
checkString += "c1";
var TFc1:TextFormat = Object(this).sopa.c1.letra.getTextFormat(0,1);
TFc1.color = 0xff0022;
Object(this).sopa.c1.letra.setTextFormat(TFc1);
check();
}
but whit that many buttons (more than 100) is to long writing them one by one.
So i think put the values (instance names) of the buttons in an array. Something like this
var casillas:Array = [a1, b1,c1];
for (var i:uint = 0; i < casillas.length; i++)
{
casillas[i].addEventListener(MouseEvent.CLICK, a1presion);
trace(casillas[i])
}
but i cant get the value of the array element just a [object SimpleButton].Have anyone any ideas how to do this: I'm new in as3 i worked all my life in as2 and this is way to hard to understand for me.
This is where Object Oriented Programming shines. You encapsulate all properties and methods into a single object. You can then create as many of these objects, and watch as they function individually with the behavior specified inside.
Here's a simple button class I sometimes use myself:
package buttons {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class SimpleButton extends MovieClip {
public function SimpleButton() {
addEventListener(MouseEvent.MOUSE_OVER, mOver);
addEventListener(MouseEvent.MOUSE_OUT, mOut);
addEventListener(MouseEvent.MOUSE_DOWN, mDown);
buttonMode = true;
}
protected function mOver(e:MouseEvent):void {
gotoAndStop(2);
}
protected function mOut(e:MouseEvent):void {
gotoAndStop(1);
}
protected function mDown(e:MouseEvent):void {}
}
}
Then create as many instances as you like, and store them in some container so that you can get rid of them at some point in the destruction of the program.
var buttons:Vector.<SimpleButton> = new Vector.<SimpleButton>();
buttons.push(new SimpleButton());
buttons.push(new SimpleButton());
buttons.push(new SimpleButton());
Firstly, I have to agree with Iggy, what you are trying to do will ultimately be more re-usable and easy to understand if it were written using Object Oriented code.
However, from what you have already written, I think you are saying is that you are struggling to create an event handler that is generic to all your buttons? So if you need to get the string name of the MovieClip as well as the actual clip reference maybe you could do something like this:
var casillas:Array = ["a1", "b1", "c1"]; //Have an array of the names of the buttons
for (var i:uint = 0; i < casillas.length; i++)
{
var mcReference:MovieClip = this.sopa[ casillasNames[i] ]; //Use the string reference to get hold of the button
mcReference.btn.addEventListener(MouseEvent.CLICK, _handleButtonClick); //Add a listener to the same "btn" child of the casilla as you did before.
}
you need a listener
function _handleButtonClick(e:Event)
{
var buttonReference:* = e.target;
So e.target in this instance should be the same object that you added to the array earlier (a1 or b1 or c1 depending on which one is clicked)
Now I'm going to make some leaps about how those MovieClips (a1,b1,c1) are structured (I'm guessing they are all the same just with different data in?)
function _handleButtonClick(event:MouseEvent):void
{
var clickedButtonReference:* = e.target;
for(var i:int = 0; i< casillas.length; i++)
{
if(this.sopa[ casillasNames[i] ].btn == clickedButtonReference) //Check each button on the stage to see if it is the one that has been clicked.
{
checkString += casillasNames[i];
//To find this bit "this.sopa.c1.letra" there are two options either
var selectedLetra = this.sopa[ casillasNames[i] ].letra; //Using the string name of the MC to find it.
//or
var selectedLetra = clickedButtonReference.parent.letra; //Or using the fact that we know that the original MC contains both the btn and the letra as children.
//Once we have it though the rest of your function should work ok.
var TFc1:TextFormat= selectedLetra.getTextFormat(0,1)
TFc1.color = 0xff0022;
selectedLetra.setTextFormat(TFc1);
check();
//Just add a break to stop searching for the button since we have found it.
break;
}
}
}
[AS3.0] I'm currently working on my graduation diploma and I have a problem with managing movieClips with the same instance name..sort of. I'm creating an array of objects with this Class an Arrays in frame 1:
package
{
import flash.display.MovieClip;
public class createFractionBuilding
{
public function createFractionBuilding(_fractionBuildingMovieClip:MovieClip, _fractionBuildingLevel:Number,_fractionBuildingCost:Number, _fractionBuildingFrame:Number)
{
fractionBuildingLevel = _fractionBuildingLevel;
fractionBuildingCost = _fractionBuildingCost;
fractionBuildingMovieClip = _fractionBuildingMovieClip
fractionBuildingFrame = -fractionBuildingFrame
}
public var fractionBuildingLevel:Number,fractionBuildingCost:Number,fractionBuildingFrame:Number;
public var fractionBuildingMovieClip:MovieClip
}
}
On main timeline I'm refering to this Class by creating an Array of objects:
var allHumanBuildingsList:Array = new Array();
var humanCapitolBuildingProperties:createFractionBuilding = new createFractionBuilding(humanCapitol_mc,1,1000,1);
var humanCastleBuildingProperties:createFractionBuilding = new createFractionBuilding(humanCastle_mc,2,1000,1);
allHumanBuildingsList.push(humanCapitolBuildingProperties);
allHumanBuildingsList.push(humanCastleBuildingProperties);
For this to work I have to have movieclip called "humanCapitol_mc" or "humanCastle_mc" in this frame (1).
But in frame (2) I want to refer to this movieClip with such function:
function humanBuildingLevelCheck()
{
for (var a:Number = 0; a < allHumanBuildingsList.length; a++)
{
trace (String(allHumanBuildingsList[a].fractionBuildingMovieClip))
if (allHumanBuildingsList[a].fractionBuildingLevel == 2){
allHumanBuildingsList[a].fractionBuildingMovieClip.gotoAndStop(3)
} else if (allHumanBuildingsList[a].fractionBuildingLevel == 1){
allHumanBuildingsList[a].fractionBuildingMovieClip.gotoAndStop(2)
}
}
}
humanBuildingLevelCheck()
(Each MovieClip on first frame is blank, on second is one picture, on third is different one)
Everything works, when the array creation code is in frame two, but it won't work the way I want: first I create variables and objects and then I change it's values dynamically. Do you have an idea, how could that work?
Make it global. If one variable is declared in keyframe 1, you can't see it in keyframe 2 but if you declare it in one layer that has only one keyframe then it will be visible anywhere within that timeline.
Another desperately stuck first year student here. And I have to use FlashCS as my coding environment. And it sucks. So I'll try some well constructed and clear questions. There is:
public var object: symbol1;
public var objectarray: Array = new Array();
in my main. Then a function there that uses a timer and spawns a new object and pushes it onto the array:
object = new symbol1;
objectarray.push(object)
but then when I trace() the .length of the array it displays TWO numbers of the array length every timer period in the output. As in:
1 1 2 2 3 3
etc. This is my first mystery. Why two not one? because there is no way I'm calling the function that includes the trace () twice. Also I think I need to be able to remove my object from the objectarray when it goes off the stage, but the objectarray.pop() doesn't seem to work if I use it like so in a function:
if (object.y == stage.stageHeight)
objectarray.pop()
As in I try trace() the array.length before and after the .pop(), but it just keeps going up by one every timer period.
And the other, bigger issue is I want to know if you are allowed to put the .addEventListeners that you usually place right under the main function of any class into a statement loop. As in I've got
class extends Main {
class function() {
for (var i:Number = 0; i < objectarray.length; i++){
objectarray[i].addEventListener(Event.ENTER_FRAME, collision);}}
And then, if it is allowed, the program doesn't seem to enter the collision function of this same class anyway.
function collision (event:Event) : void{
if (this.hitTestObject(object)){
trace('hit');}}
so I searched and ended up adding a
var clip:MovieClip = MovieClip(e.target);
in the first line of the function, but then it didn't work and I realized I on't understand what's it meant to do, what's going on anymore and what is the syntax for this casting.
Thank you very much.
Edit/Update: adding more of my code eventhough I hate copypasting it like this. This is the symbol class that is going to change when an object of another class hits it
public class Head extends Main {
public function Head(){
this.stop();
for (var i:Number = 0; i < nicesnowflakearray.length; i++){
nicesnowflakearray[i].addEventListener(Event.ENTER_FRAME, snowhit);
}
}
public function snowhit(event:Event) : void {
if (this.hitTestObject(nicesnowflake)){
//I changed this line to (e.currentTarget.hitTestObject(nicesnowflake)) as Atriace suggested, but nothing changed, and I just don't understand why my version wouldn't work.
trace("hit");
}
}
And this is the class that spawns the objects that are supposed to hit the Head object:
public class Main extends MovieClip {
public var nicesnowflake: fallingsnow;
var nicesnowflakespawntimer: Timer = new Timer(1000);
public var nicesnowflakearray: Array = new Array();
public function Main() {
nicesnowflakespawntimer.addEventListener(TimerEvent.TIMER, nicesnowflakespawn);
nicesnowflakespawntimer.start();
}
public function nicesnowflakespawn(event:TimerEvent) : void {
nicesnowflake = new fallingsnow;
nicesnowflake.x = Math.random()* stage.stageWidth;
nicesnowflake.y = - stage.stageHeight + 100;
nicesnowflakearray.push(nicesnowflake);
stage.addChild(nicesnowflake);
trace(nicesnowflakearray.length);
}
Why two, not one?
Anytime you extend another class, it implicitly calls the parent class' constructor. We know this as super(), and can be quite handy. This is why you're getting doubles on your trace statement. Technically, you can choose not to call super().
.pop()
It should remove the last element from that array, however, I'm thinking that if an arbitrary object leaves the stage, you can't be gauranteed it'll be the last element. Ergo, you want splice()
if (object.y == stage.stageHeight) {
objectarray.splice(objectarray.indexOf(object), 1)
}
Event Listeners
I didn't follow your quandary, so I'll just try to rewrite what I think you were trying to do.
package {
import flash.display.MovieClip;
public class Main extends MovieClip {
private var objectarray:Array = []; // Note, I haven't populated it with anything, I'm assuming you have.
private var theWall:MovieClip = new MovieClip(); // I haven't added this to the stage, or given it shape. You need to for hitTestObject to work.
public function Main() {
// This is your constructor.
for (var i:Number = 0; i < objectarray.length; i++) {
objectarray[i].addEventListener(Event.ENTER_FRAME, collision);
}
}
private function collision(e:Event):void {
if (e.currentTarget.hitTestObject(theWall)) {
trace('hit');
}
}
}
}
Of note, you may want to look at a guide to hitTestObject() if it's giving you issues.
In a game I'm making, I'm trying to have sprites put on top of a hit box. The best way I could think of accomplishing this is to make two arrays; one for the hit boxes, and one for the sprites, and then have the sprites stay on top of their respective hit-box via a for loop that will be in its own script.
Problem is, is that when I try to get the MovieClips in either of the arrays to do anything, it doesn't work. If I do a trace on the X or Y location of the sprites, I get "undefined" in my terminal. I'm going to explain this from the top down.
Here is an excerpt from my class that contains the for loop (Dasengine is my main class fyi)
for(var i:Number = 0; i < Dasengine.ovrcnt.length; i++){
trace(Dasengine.ovrcnt[i].x); //returns "undefined"
trace(Dasengine.ovrcnt[i]); //returns "object Onmea"
Dasengine.ovrcnt[i].x = Dasengine.enemycnt[i].x;//this isn't working
}
In another script when an enemy spawns, they go through this method.
if(ENEMY SPAWN CONDITION IS MET ){
Dasengine.baddie = new nme_spawn.Enemya(); //ENEMY HITBOX
Dasengine.Obaddie = new nme_overlay.Onmea(); //ENEMY's sprite
Dasengine.enemycnt[cntup] = [Dasengine.baddie]; //Enemy's Hit box movie clip is put in array meant for holding enemy movie clips
Dasengine.ovrcnt[cntup] = [Dasengine.Obaddie]; //Enemy sprites that go over the hit boxes are stored here
cntup++; //this is so movie clips are put in new parts of the array
}
in my main class, the movie clips are declared as follows also I have the addChild functionality in there.
public static var Obaddie:nme_overlay.Onmea;
//^variable for sprite
public static var baddie:nme_spawn.Enemya;
//^variable for hitbox
also Obaddie= Overlay baddie. Its the MovieClip that acts as what goes on top of the hitbox, this is what the player will see
badde = is simply the hitbox. This contains the "meat" of the enemy ai.
I talked about this with some friends and they thought I might need to define what 'X' is inside of the class of the object that is in the array. So I did the following
package nme_overlay {
import flash.display.*;
import flash.events.*;
import nme_spawn.*;
public class Onmea extends MovieClip{
// Constants:
// Public Properties:
// Private Properties:
public static var xloc:int = 0;
// Initialization:
public function Onmea() {
this.addEventListener(Event.ENTER_FRAME, overlaya);
}
private function overlaya(e:Event){
xloc = 55;
//trace(xloc);
}
}
}
and then for the looping class I did this
for(var i:Number = 0; i < Dasengine.enemycnt.length; i++){
trace(Dasengine.ovrcnt[i]);//returns "object Onmea"
trace(Dasengine.ovrcnt[i].xloc);//still returns "undefined"
}
Your xloc variable is static--it belongs to nme_overlay, the Class, not any particular instance. If you were to do this in your code AND you had strict mode on (which I suspect you do not, because there's a lot of stuff in your code that should be giving you at least warnings), you'd get an error that would tell you exactly that:
for(var i:Number = 0; i < Dasengine.enemycnt.length; i++){
trace(Dasengine.ovrcnt[i]);//returns "object Onmea"
trace(nme_overlay(Dasengine.ovrcnt[i]).xloc);//still returns "undefined"
}