AS3 how do I control a movieclip in a peer to peer instance swf? - actionscript-3

I've made a app using netconnection and netgroup in Flash CS6. I'm trying to build a simple 2-player multiplayer game. When 2 two players are connected I would like to hide a movieclip in one of the instances of the swf but not the other. How is that done?
It's a turn-based game so when player 1 takes his turn player 2 must not be able to click a button (so I want to hide it) and vice-versa.

I think it's working now. In the move function I added ok_mc.visible=false which hides the mc in the "local" instance and the put ok_mc.visible=true in the netstatus event. Like this
function drop(e:MouseEvent):void {
this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveMe);
// Save the current movieclip position
var obj:Object = {};
obj.x = mc.x ;
obj.y = mc.y ;
obj.activePlayer=players[aktiv-1]
ok_mc.visible=false;
// Set the peerID to a group address suitable for use with the sendToNearest() method.
obj.sender = group.convertPeerIDToGroupAddress(nc.nearID);
obj.id = new Date().time;
// Sends a message to all members of a group.
group.post(obj);
}
function netStatus(event:NetStatusEvent):void{
switch(event.info.code){
case "NetGroup.Posting.Notify":
mc.x = event.info.message.x;
mc.y = event.info.message.y;
ok_mc.visible=true;
break;
}
}
It seems to work. Now I just have to figure out to show it at init in the first instance. Is there a way to count groupmembers and/or loop through them?

Related

How Can I Make AS3 Var Loop move speed on the Y Axis on repeat my university project

I'm tring to get the car image to move down the screen on the y Axis and make it a repeat and colide with another object
//creates the new Car
for (var c:int=0; c<8; c++){
var newcar = new car();
newcar.x = 55*c;
newcar.y = 100;
EntityArray.push(newcar);
stage.addChild(newcar);
trace("Car Created"+c)
}
How to make it colide with the following and remove it from screen
//creates the new Frog
for (var f:int=0; f<1; f++){
var newfrog = new frog();
newfrog.x = 210;
newfrog.y = 498;
EntityArray.push(newfrog);
stage.addChild(newfrog);
trace("Frog Created"+f)
}
[image][1][1]: https://i.stack.imgur.com/Ihsfx.png
Though I'm quite pleased to hear that today they still tell you about ActionScript at college, it's a bit hard
to give you advice here since I don't know what they've covered yet.
Generally speaking, you could realize this with a simple game loop, that runs periodically and in it's most simple
form:
checks user input (in your case most likely pressing left/right to move the frog)
update game state (move the cars & the frog ; check for collision)
draw everything to screen
For creating the periodical loop, Flash/ActionScript offers a powerful event called ENTER_FRAME. Once started, it
will fire with the framerate of the movie. So if you set your movie to 60fps, it will execute it's callback function
roughly every 17ms.
I assume your instances of Frog and Car extend Flash's Sprite or MovieClip class - so collision detection is also pretty
easy since you can use the inherited hitTestObject() method.
To make things a bit easier though I'd recommend that you don't put the reference to the frog instance inside the EntityArray.
Better use a global reference. (Also, you don't need a for-loop because there's just one frog)
As another sidenote, it's quite common that classnames start with a capital letter.
private var newfrog:frog; // defines a class variable we can access anywhere inside our class
//Later on instantiate new cars and the frog:
for (var c:int=0; c<8; c++){
var newcar = new car();
newcar.x = 55*c;
newcar.y = 100;
EntityArray.push(newcar);
stage.addChild(newcar);
}
newfrog = new frog();
newfrog.x = 210;
newfrog.y = 498;
stage.addChild(newfrog);
addEventListener(Event.ENTER_FRAME, loop); // register an ENTER_FRAME listener for the main game loop
private function loop(e:Event):void
{
var tempCar:car;
for(var a:int=0;a<EntityArray.length;a++)
{
tempCar=EntityArray[a]; // get a car from the EntityArray
tempCar.y++; // move it down on screen
if(tempCar.y>600) // if it's vertical position is greater than 600...
{
tempCar.y=0; // ...move it back to the top
}
if(newfrog.hitTestObject(tempCar)) // evaluates to true, if a car and the frog's bounding boxes overlap
{
trace("there's a collision!"); // do something
}
}
}

Actionscript 3 - Passing one variable from one object to another in the same project

I'm fairly new at ActionScript 3 but I am working on it and trying to learn by reading and modifying ActionScript source codes.
So far so good, however I stumbled upon one problem which I seemingly can't solve by myself. It should be faily simple for you guys tho.
The situation:
I got one "object" which is clickable and gives a random value which is also being saved in a variable.
I got another "object" which does the same thing but only has a different name.
I want the variable from the first object to be passed to the second one, how can I do it?
One way to pass values relies on using references to objects...
// mc1 and mc2 exist as movieclips on the stage
mc1.addEventListener( MouseEvent.CLICK, onClick );
mc2.addEventListener( MouseEvent.CLICK, onClick );
function onClick( event:MouseEvent ):void
{
// reference clicked movieclip through click target
var mc:MovieClip = event.target as MovieClip;
// if our clip matches one, assign the other clip the value.
if ( mc === mc1 )
{
mc2.value = mc.value;
}
else if ( mc === mc2 )
{
mc1.value = mc.value;
}
}
There's a thousand ways to pass references around, and this is just one.
OK guys let me show you the important parts of the code (cant show it all since I paid for it and I don't know whether the author woudl be OK with me publishing all of it).
I got these two objects there. When I click on one of the objects (which is a DICE), it gives me this code basically
var faceValue:int = 6;
// Add mouse click functionality to roll the die
addEventListener(MouseEvent.CLICK, onClickDie, false, 0, true);
mouseChildren = false;
buttonMode = true;
function onClickDie(e:MouseEvent):void {
removeEventListener(MouseEvent.CLICK, onClickDie);
buttonMode = false;
// Initiate the roll-out sequence
if(faceValue == 6) {
gotoAndPlay("rollout6");
etc...
Then somewhere in the frame where it is SPINNING the dice it is randomizing a number and save it into facevalue
// Calculate a random face value between 1 and 6
faceValue = 1 + Math.floor(Math.random()*6);
// Initiate the roll-in sequence
if(faceValue == 6) {
gotoAndPlay("rollin6");
...etc
Now how can I get the randomized facevalue from the spinning the dice frame to pass it to the other dice?

Making multiple objects draggable

I have about 50 symbols that I want to make draggable. Nothing fancy, just the ability to click it and drag it to a different location.
I found as3 code for doing so but when I paste it into my file it gives me errors:
**Error** Scene=Scene 1, layer=Units, frame=1:Line 9: The class or interface 'MouseEvent' could not be loaded.
function mouseDownHandler(evt:MouseEvent):void {
That code is:
// Register mouse event functions
fighter_uk.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
fighter_uk.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
fighter_uk.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
fighter_uk.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
// Define a mouse down handler (user is dragging)
function mouseDownHandler(evt:MouseEvent):void {
var object = evt.target;
// we should limit dragging to the area inside the canvas
object.startDrag();
}
function mouseUpHandler(evt:MouseEvent):void {
var obj = evt.target;
obj.stopDrag();
}
I'm using flash pro 8, so I tried finding as2 code but couldn't find it.
Also, is there an 'easy' way to code all 50 objects?
I think you're trying to compile AS3 code with AS2 compiler. Try changing your compilation settings to target AS3.
Also you may need to include the class import at the top of your code:
import flash.events.MouseEvent;
To drag 50 objects, add them all on the same container sprite and add the listener to the container sprite only:
var holder:Sprite = new Sprite();
for ( var i:int = 0, l:int = 50; i < l; i++ ) {
var dragee:YOUR_CUSTOM_OBJECT = new YOUR_CUSTOM_OBJECT();
holder.addChild(dragee);
}
addChild(holder);
holder.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
holder.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
holder.addEventListener(Event.MOUSE_LEAVE, mouseUpHandler);
var currentDragee:YOUR_CUSTOM_OBJECT = null;
function mouseDownHandler(evt:MouseEvent):void {
currentDragee = evt.target as YOUR_CUSTOM_OBJECT;
if ( currentDragee !== null ) {
currentDragee.startDrag();
holder.addChild(currentDragee); // bring current to front position
}
}
function mouseUpHandler(evt:Event):void {
if ( currentDragee !== null ) currentDragee.stopDrag();
currentDragee = null;
}
YOUR_CUSTOM_OBJECT being the object class you need to drag. Hope it helps!
This page seems to have the answers you are looking for (AS2 drag and drop). If you've already seen it, you'll need to explain why it's not good enough for your needs.
If you want to drag/drop multiple instances in AS2, you can still add the code to the movieClip symbol, export it from the library and load the instances up using attachMovie (all 50 of them). If they are all different, then attach the code as necessary to the clips themselves, or to some function elsewhere that will capture all the clicks and decide what was clicked. This is all very doable in AS2.
Remember you can use your onClipEvent(load) function to set up a lot of the initial lifting.
Here's a sample I made in AS2 for making a node tree. It's all draggable (mouse drag) and zoomable (with mouse Wheel). You can add nodes by clicking on the little down arrow in the node box. Each node is listening for the mouse.
You'll want to look at this section for the most part:
// Enable drag on button press
on (press)
{
startDrag(this);
}
// Stop the drag on release of mouse button
on (release)
{
stopDrag();
}
Besides this, I'm not really sure how your setup looks, so I hope this helps get the ball rolling. (Check the link, there's lots of little gems in there).
Flash Professional 8 only supports ActionScript 2 & 1
You can follow this official URL and learn how to do that in ActionScript 2, but I extremely recommend you to work with ActionScript 3.

Remove Object when Collision Detected in AS3

I'm making a game where your player (gun) shoots enemies (baddie) to gain score. I've set it up so you have three lives (lives) until it is game over. I am trying to add an addLife function that works by when your player collects an object (waterMelon), it gets a life. I have added a timer event that runs the function addLife, that adds an instance of the Movie clip to the stage every 5 seconds. In that function, ive created another function called checkCollisions, that is meant to check the collision of the player, to the object, remove the object from the stage, then add a life.
var timer2:Timer = new Timer(5000);
timer2.addEventListener(TimerEvent.TIMER, addLife);
timer2.start();
var watermelon:waterMelon = new waterMelon();
function addLife(evt:TimerEvent):void{
watermelon.x = Math.random() * stage.stageWidth;
watermelon.y = Math.random() * stage.stageHeight;
addChild(watermelon);
watermelon.push(waterMelon);
checkCollision();
}
function checkCollision(){
if (gun.hitTestObject(watermelon)){
removeChild(watermelon);
lives++;
livesDisplay.gotoAndStop(livesDisplay.currentFrame-1);
}
}
The only part of the code that works is the adding the watermelon to the stage, but when my player collides with it, it does not remove from the stage. Could someone please tell me how I make my watermelon completely be removed from the stage when the player collides with it, and add a life to my player. Once again, each object equals this; gun = player, waterMelon = object/watermelon, lives = lives, livesDisplay = physical display of lives.
You have tried to delete the array(watermelon) from the stage instead of the object(waterMelon). Although by the looks you are using arrays so you will have to find the index of the object colliding with the gun from the array watermelon. You do this with a for loop.
for (melonCount:int = 0; melonCount>watermelon.length;melonCount--)
{
if (gun.hitTestObject(watermelon[melonCount]))
{
removeChild(watermelon[melonCount])
watermelon.splice(melonCount, 1)
melonCount--
}
}

globaltoLocal in addchild

I have an enemy that adds an attack movie clip. More specifically, this attack movie (We'll call it masterAttack) clip is a blank movie clip that acts like a super class, that will hold other attacks like a weak and strong attack. So when my enemy attacks using a timer, it adds the masterAttack on a global to local point.
Below is the Enemy timer attacking a tile the player is on:
if (Main.tileset[k].tileMiddle.hitTestObject(Main.player.visionPoint))
{
this.addChild(masterAttack);
var pt:Point = this.enemymagic.globalToLocal(new Point(Main.tileset[k].x, Main.tileset[k].y));
masterAttack.masterEnemyAttackTimer.start();
this.masterAttack.x = (pt.x);
this.masterAttack.y = (pt.y);
}
And Below is the masterAttack timer:
function mastertimer(event:TimerEvent) {
addChild(sludgeball); //This is one of the many attacks pickable by the masterAttack
sludgeball.enemyattackTimer.start();
if (this.sludgeball.currentLabel == "End") {
this.sludgeball.gotoAndPlay(1);
masterEnemyAttackTimer.stop();
if (masterEnemyAttackTimer.running == false)
{
attackStop = true;
this.parent.removeChild(this);
removeChild(sludgeball);
}
}
My problem is, on the first run, the masterAttack will attack the player wherever it is, then remove itself, which is good. Then the next time it runs, the masterAttack is not hitting the player. It's as if the globaltoLocal isn't working after the first run.
this.addChild(masterAttack);
var pt:Point = this.enemymagic.globalToLocal(new Point(Main.tileset[k].x, Main.tileset[k].y));
You add the masterAttack to this, but you got pt using enemymagic's globalToLocal
So change the line like this
var pt:Point = this.globalToLocal(new Point(Main.tileset[k].x, Main.tileset[k].y));