00:00
00:00
Newgrounds Background Image Theme

LewgusWithFriends just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS3 objects communicate?

582 Views | 19 Replies
New Topic Respond to this Topic

AS3 objects communicate? 2014-09-03 22:54:53


is there any way to pass a variable between two movieclips without using public variables? I don't want to mess around with classes just to let two movieclips communicate


...

Response to AS3 objects communicate? 2014-09-04 02:04:20


At 9/3/14 10:54 PM, Xombiehacker wrote: is there any way to pass a variable between two movieclips without using public variables? I don't want to mess around with classes just to let two movieclips communicate

There's a general rule in programming that parents are allowed to instructed their children, children shouldn't instruct their parents, and children should not instruct other children they are not the parent of. Why would you need this behavior? It's bad organization.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to AS3 objects communicate? 2014-09-04 07:11:37


At 9/3/14 10:54 PM, Xombiehacker wrote: is there any way to pass a variable between two movieclips without using public variables?

There are a few ways to let objects communicate each other.
If you could provide more information about what those objects are, what they are communicating, etc. it would be easier to answer your question.

I don't want to mess around with classes just to let two movieclips communicate

You are already messing around with classes and objects.

Response to AS3 objects communicate? 2014-09-04 07:27:18


It's a pain in the ass but MVC is what you're looking for

Response to AS3 objects communicate? 2014-09-04 07:57:21


I can't tell if this means you're working with AS2 instead now, so I'm not entirely sure of the syntax.

What you could do, however you are coding, is to have the parent take information from one child and pass it to another.

Say you have variable "coins:int = 5" in an object called "moneyBag" and a variable "account:int = 0" in an object called "bank".

Your parent could "transfer" the coins from your bag into your back like so:

public function transferCoins(amount){
  if(amount <= moneyBag.coins){
    bank.account += amount;
    moneyBag.coins -= amount;
  }
}

It's definitely the best way I've found to do what you seem to want.

Response to AS3 objects communicate? 2014-09-04 17:32:24


At 9/4/14 02:04 AM, MintPaw wrote: There's a general rule in programming that parents are allowed to instructed their children, children shouldn't instruct their parents, and children should not instruct other children they are not the parent of. Why would you need this behavior? It's bad organization.

you're getting way too metaphorical here 0.o I just want to be able to let objects communicate, IE two movieclips on stage joe and bob, bob moves to the left if joe has $30 in his wallet, or something like that?
like
//code within movieclip bob
if(root.joe.money = 30){
this.x -= 10;
}


...

Response to AS3 objects communicate? 2014-09-04 17:50:00


At 9/4/14 07:27 AM, slugrail wrote: It's a pain in the ass but MVC is what you're looking for

MVC?


...

Response to AS3 objects communicate? 2014-09-04 18:04:07


At 9/4/14 05:32 PM, Xombiehacker wrote: you're getting way too metaphorical here 0.o

Yeah but there's a reason it's not obvious how to do this.

Why can't the root(or any other parent) just do this?

if(joe.money == 30) bob.x -= 10;

Don't purposely create an unorganized mess.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to AS3 objects communicate? 2014-09-04 18:48:32


At 9/4/14 05:50 PM, Xombiehacker wrote:
At 9/4/14 07:27 AM, slugrail wrote: It's a pain in the ass but MVC is what you're looking for
MVC?

model view controller, go look it up
It's a design pattern. The next step of abstraction beyond classes.

Response to AS3 objects communicate? 2014-09-04 18:49:32


At 9/4/14 05:50 PM, Xombiehacker wrote:
At 9/4/14 07:27 AM, slugrail wrote: It's a pain in the ass but MVC is what you're looking for
MVC?

MVC is a coding paradigm pattern.
It's a philosophically different way of writing code.

Trust me when I say that's not the road you want to go down right now.

This is an XY problem I highly suggest you read that page, but here's a quick summery.

User wants to do X.
User doesn't know how to do X, but thinks they can fumble their way to a solution if they can just manage to do Y.
User doesn't know how to do Y either.
User asks for help with Y.
Others try to help user with Y, but are confused because Y seems like a strange problem to want to solve.
After much interaction and wasted time, it finally becomes clear that the user really wants help with X, and that Y wasn't even a suitable solution for X.

So what is the real issue you're having?


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to AS3 objects communicate? 2014-09-07 02:08:18


At 9/4/14 06:49 PM, MintPaw wrote: So what is the real issue you're having?

It's an issue in several parts of my code, and I don't mean to smash AS3 again, but if AS2 had the capability to pass information seamlessly between two movie clips via movieclip2.variable = movieclip1.variable, I don't understand why they removed it, again, I'm not trying to smash AS3 I'm just frustrated. (really, the more I've been working with it the more I realize that though it doesn't have some abilities that it's predecessor had, it introduces things that AS2 lacked)
If you would like an example here:
I have a superman type game, where the character can smash through walls but only if the force they exert is great enough, where force is derived from speed they're moving, but there's no native variable to test an objects x and y speeds, so I have to set a variable, and when that variable is nested inside the character, and the wall needs to know what that variable is to know weather or not it should break, and it can't get access to that variable.... you know what I'm saying?


...

Response to AS3 objects communicate? 2014-09-07 13:09:50


This can be done easily. And works exactly the same as my example.

if (player.hitTestObject(wall))
{
	if (player.forceVariable > requiredForce)
	{
		wall.break();
	}
}

I'm not sure why you'd could come to this solution, you must be extremely conditioned to AS2.

And it's not the the feature is missing, it's there in plain sight, I'm sure everyone that's posted in this thread know it. They either don't want to tell you because it's such a bad practice. Or they simply never thought of it because of good programming practice.

So why is it a bad? So let's say you have a large complex platformer game with lots of enemies and projectiles. And let's say you encounter a glitch in which the player's health is being drained for no reason. With you methodology of cross accessing variables it could be anything. You'll have to looks though each enemy and projectile that could be tampering with the player's health value and test it. That's only if you're lucky, it could have something to do with a item that wasn't meant to do damage. Then you'd have to look through literally every line of code on each object.

Now in the our general way what would happen is the enemies would pass an even when they wanted to do damage, this event would be collected by "root" or whatever's the main parent of the game, then it would inflict the damage on the player. So you encountered this glitch then all you would have to do is go to your function on the main parent and put some traces in the damage function to determine the error.

The point is when there's no restrictions there's no organization.

Also, not "feature" that AS3 doesn't have is a bad thing. Here's an example, go into AS2 and run this line:

sdff3.fsdf3(asdsa324).sad3q(sda34324[sd42dx]).asda = sad3(sfcx[Sree3]);

What's the error? There's no error. This mean that any tiny typo in your code will be completely ignored and you'll have to hand pick through every line when you find that something doesn't work

AS3 wouldn't let you do that, it give you 7 errors pointing out each item that doesn't exists. It's not a "feature" that AS2 doesn't check for existence. And it wasn't "removed" when AS3 requires object to exist before accessing them.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to AS3 objects communicate? 2014-09-07 13:49:13


At 9/7/14 02:08 AM, Xombiehacker wrote: I have a superman type game, where the character can smash through walls but only if the force they exert is great enough, where force is derived from speed they're moving, but there's no native variable to test an objects x and y speeds, so I have to set a variable, and when that variable is nested inside the character, and the wall needs to know what that variable is to know weather or not it should break, and it can't get access to that variable.... you know what I'm saying?

MintPaw's right in every respect. Since he didn't give you a real solution, though, here it is. I am assuming a platformer.
(this is all sans class declarations because I can't be arsed)

LevelEngine.as

private var player:Player = new player();
private var enemies:Vector.<Enemy> = new Vector.<Enemy>();
private var breakableObjects:Vector.<BreakableObject> = new Vector.<BreakableObject>();
private var tiles:Vector.<Tile> = new Vector.<Tile>();

public function LevelEngine():void {
  //fill vectors
}

//called every frame using an enterFrame event
public function update(e:Event):void {
  var i:uint;
  
  player.x += player.velocityX;
  player.y += player.velocityY;
  
  for (i = 0; i < breakableObjects.length; i++) {
    if (CollisionEngine.collidesX(player, breakableObjects[i])) {
      if (player.velocityX > 3) {
        breakableObjects[i].break();
      } else {
        player.x = (player.x < tiles[i].x) ? tiles[i].x - player.width : tiles[i].x + tiles[i].width;
      }
    } else if (CollisionEngine.collidesY(player, breakableObjects[i])) {
      if (player.velocityY > 3) {
        breakableObjects[i].break();
      } else {
        player.y = (player.y < tiles[i].y) ? tiles[i].y - player.height : tiles[i].y + tiles[i].height;
      }
    }
  }
  for (i = 0; i < tiles.length; i++) {
    if (CollisionEngine.collidesX(player, tiles[i]) {
      player.x = (player.x < tiles[i].x) ? tiles[i].x - player.width : tiles[i].x + tiles[i].width;
    }
    if (CollisionEngine.collidesY(player, tiles[i]) {
      player.y = (player.y < tiles[i].y) ? tiles[i].y - player.height : tiles[i].y + tiles[i].height;
    }
  }
  for (i = 0; i < enemies.length; i++) {
    if (CollisionEngine.collides(player, enemies[i])) {
      player.health--;
    }
  }
}

This is an inline if statement. It acts as an if/else statement. The condition to fill in in the parenthesis, the "if" condition after the question mark, and the "else" condition after the colon.

player.y = (player.y < tiles[i].y) ? tiles[i].y - player.height : tiles[i].y + tiles[i].height;

Also assume CollisionEngine was already built since there's a million and one ways to handle collisions.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to AS3 objects communicate? 2014-09-07 14:05:45


Platformers are difficult, and as such I would fix my collision handling before I used that code.

things like

player.x = (player.x < tiles[i].x) ? tiles[i].x - player.width : tiles[i].x + tiles[i].width;

should actually be handled like such:

player.x -= player.velocityX;
player.velocityX = 0;
player.x = (player.x < tiles[i].x) ? tiles[i].x - player.width : tiles[i].x + tiles[i].width;

and the same with all of them, including the Y collisions. This prevents the engine checking more collisions that needed and also accidental "teleportation" in case the walls aren't wide and the velocity is too high.

I would highly recommend raycasting collision detection from the current player's point to the player's previous point (current point - velocity) in the CollisionEngine because, again, of velocity being too high and walls being too narrow; which, again, would result in teleportation. box/box (aabb) and pixel-perfect collision won't work here.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to AS3 objects communicate? 2014-09-08 22:18:22


At 9/7/14 01:09 PM, MintPaw wrote: wall.break();

right here aren't you accessing variables from the wall?

And it's not the the feature is missing, it's there in plain sight

Where?

Then you'd have to look through literally every line of code on each object.

You're scorning me about having bad practices when in your example you wouldn't test the game frequently enough to even know what bits of code you've recently altered? I've never come across this glitch due to good organization

Now in the our general way what would happen...

I'm really not even sure what you're talking about here, but you must have had some really bad experiences with AS2 to get these types of glitches

The point is when there's no restrictions there's no organization.

it's called self discipline?

Also, not "feature" that AS3 doesn't have is a bad thing.

A lack of anything is a bad thing... ever hear the phrase "rather have it and not need it, then need it and not have it"?

AS3 wouldn't let you do that, it give you 7 errors pointing out each item that doesn't exists. It's not a "feature" that AS2 doesn't check for existence. And it wasn't "removed" when AS3 requires object to exist before accessing them.

I'm not even looking at that o.0 I do like that about AS3, I'm complaining about the fact that movieclip1 can't access movieclip2.variable


...

Response to AS3 objects communicate? 2014-09-08 23:38:17


At 9/8/14 10:18 PM, Xombiehacker wrote: I'm not even looking at that o.0 I do like that about AS3, I'm complaining about the fact that movieclip1 can't access movieclip2.variable

Parents and observers access children, and that is it. Children do not access children.


Programming stuffs (tutorials and extras)

PM me (instead of MintPaw) if you're confuzzled.

thank Skaren for the sig :P

BBS Signature

Response to AS3 objects communicate? 2014-09-09 00:11:51


At 9/8/14 10:18 PM, Xombiehacker wrote:
At 9/7/14 01:09 PM, MintPaw wrote: wall.break();
right here aren't you accessing variables from the wall?

Yes, the parent object is accessing a method from a child.

And it's not the the feature is missing, it's there in plain sight
Where?

The same exact way it's done in AS2.

Then you'd have to look through literally every line of code on each object.
You're scorning me about having bad practices when in your example you wouldn't test the game frequently enough to even know what bits of code you've recently altered? I've never come across this glitch due to good organization
Now in the our general way what would happen...
I'm really not even sure what you're talking about here, but you must have had some really bad experiences with AS2 to get these types of glitches

Hard casting is there for a reason. Everyone makes mistakes.

The point is when there's no restrictions there's no organization.
it's called self discipline?

AS3 has this feature

Also, not "feature" that AS3 doesn't have is a bad thing.
A lack of anything is a bad thing... ever hear the phrase "rather have it and not need it, then need it and not have it"?

AS3 has this feature

AS3 wouldn't let you do that, it give you 7 errors pointing out each item that doesn't exists. It's not a "feature" that AS2 doesn't check for existence. And it wasn't "removed" when AS3 requires object to exist before accessing them.
I'm not even looking at that o.0 I do like that about AS3, I'm complaining about the fact that movieclip1 can't access movieclip2.variable

Ok, so you in AS3 you can do can do:
root.otherOject.variable;

Just like in AS2.

Oh wait, that give you an error? It's because "root" is a DisplayObject and DisplayObject's don't have "otherObjects".
So for the same reason that you can't just type random stuff in and have it run, this doesn't work.

So the feature about AS3 that you say you like is the same feature that you say you're saying is much worse than AS2.

You can choose to ignore that error and type:
MovieClip(root).otherObject.variable;

There you're basically telling AS3 to ignore the obvious error and just trust you. So it won't give you an error until you reach that line, then it will just try it. It doesn't matter if the object doesn't exist, it doesn't matter if the object if it has a different name.

This means you must now rigorously test this function to make sure it works. Because it could break down if it's not in this exact situation, it's also never going to be reusable. So if you really want to do it that way there it is.

You really come off as presenting yourself as some almighty being still, now even saying you're incapable of making errors due to your organization skills while bashing everything that lends to good organization. I've been using both version of actionscript for years. I've been game deving for over a decade, and I have a degree in computer science. But you're clearly right, AS2 is much worse and AS3 in every way. So with this I'm done helping you, the best of luck to you.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to AS3 objects communicate? 2014-09-09 00:12:55


At 9/9/14 12:11 AM, MintPaw wrote: Hard casting is there for a reason. Everyone makes mistakes.

Hard typing I meant.


If ya have something to say, PM me. I have a lot of time to spare.

Also never PM egg82.

BBS Signature

Response to AS3 objects communicate? 2014-09-09 03:49:31


At 9/9/14 12:11 AM, MintPaw wrote: You can choose to ignore that error and type:
MovieClip(root).otherObject.variable;

Throughout all this thread this is exactly what I was looking for, how about rather than telling someone how they should live and work, you should just tell them what they're looking for

You really come off as presenting yourself as some almighty being still, now even saying you're incapable of making errors due to your organization skills while bashing everything that lends to good organization. I've been using both version of actionscript for years. I've been game deving for over a decade, and I have a degree in computer science. But you're clearly right, AS2 is much worse and AS3 in every way. So with this I'm done helping you, the best of luck to you.

No... I'm not acting almighty, I'm just looking for some answers, but instead of giving me those answers you want to conform to your way of thinking and shove my own head up your ass, I'm actually quite glad you're done "helping me" because I've had about enough of you following me around on my own threads, go "help" someone else


...

Response to AS3 objects communicate? 2014-09-09 05:33:10


At 9/9/14 03:49 AM, Xombiehacker wrote:
At 9/9/14 12:11 AM, MintPaw wrote: You can choose to ignore that error and type:
MovieClip(root).otherObject.variable;
Throughout all this thread this is exactly what I was looking for,

That's pretty much as if you are having your first heroin shot and calling your parents to complain about that "this is what you were looking for all the time".

I'm afraid you are not interested in it, but you might want to think about what this code is doing.
Say for example, why do you cast root to something?
Why MovieClip?
Will your code work in other situations?
Debugging?
...