Need some ActionScript 3 help

New Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 29, 2003
Messages
4,765
Best answers
0
Location
The Netherlands
I have to make an assignment for school, and basically we have to make this Christmas card, made completely with ActionScript 3. And mind you, I am a complete Actionscript 3 noob.

Now, it being winter and all, I added some snow to my card. The snow works fine, but we have certain prerequisites we have to meet for our card. One of them is the usage of a Hittest.

Now what I wanted to do was add a hittest to the snowflakes with the mouse, and have the flakes bounce of the mouse.

Code:
////////////////////////////////////////////////
////////////////////////////////////////////////
///////////////SNEEUWVLOKKEN CODE///////////////
////////////////////////////////////////////////
////////////////////////////////////////////////

// Sneeuwvlokken lijst
var vlokken:Array = new Array()

//aantal sneeuwvlokken en snelheid
var aantal:int = 250;
var speed:int = 3;

var vlok:MovieClip; // maak nieuwe sneeuwvlok movieclip aan

for(var i:int = 0; i<=aantal; i++)
{
	vlok = new MovieClip(); //Nieuw sneeuwvlokje aanmaken
	vlok.graphics.clear();
	vlok.graphics.beginFill(0xCCCCCC, 1) //Kleur sneeuw
	vlok.graphics.drawCircle( -2, -2, 3 + Math.random()); // Grootte van sneeuwvlok
	
	vlok.x = Math.random() * stage.stageWidth; //Beginpunt X
	vlok.y = Math.random() * stage.stageHeight;//Beginpunt Y
	
	// zet snelheid x in vlok
	vlok.speedX = Math.random(); //Snelheid X
	// zet snelheid y in vlok
	vlok.speedY = 2 + Math.random() * 10; //Snelheid Y
	
	vlok.scaleX = Math.random() * vlok.scaleY; // Verschillend in grootte X
	vlok.scaleY = vlok.scaleX; // Neem Y grootte aan voor ronde sneeuwvlok
	
	
	// voeg toe aan displaylist
	addChild(vlok);
	
	// voeg aan sneeuwvlokken lijst toe
	vlokken.push (vlok);
}


 
stage.addEventListener(Event.ENTER_FRAME, intro); 
function intro(e:Event):void
{
     vlok.hitTestPoint(parent.mouseX, parent.mouseY, true)
     {
        trace("The mouse hits a snowflake")
     }
}  


addEventListener(Event.ENTER_FRAME, function():void
{
	for (var i:int = 0; i < vlokken.length; ++i)
	{
		var vlok:MovieClip = vlokken[i] as MovieClip;
		vlok.x += vlok.speedX;
		vlok.y += vlok.speedY;
		
		if(vlok.y > stage.stageHeight || vlok.x > stage.stageWidth)
		{
			vlok.x = Math.random() * stage.stageWidth;
			vlok.y = -10;
		}
	}


}

)
Now the problem with the code above is, whenever I start the movie, I will just get a complete list of traces, like it is just looping, without the flakes even hitting the mouse.

I'm sort of stuck now, I have tried a million things now, nothing works..
 
Former Forcepit Member :(
✔️ HL Verified
💻 Oldtimer
Joined
Aug 21, 2006
Messages
1,717
Best answers
0
Location
korriban
vlok.y = Math.random() * stage.stageHeight;//Beginpunt Y You forgot to put a space between ;// it should be ; //
 
New Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 29, 2003
Messages
4,765
Best answers
0
Location
The Netherlands
The // is a comment, so that doesn't change that much anyway. The big problem I have is that...

Code:
stage.addEventListener(Event.ENTER_FRAME, intro); 
function intro(e:Event):void
{
     vlok.hitTestPoint(parent.mouseX, parent.mouseY, true)
     {
        trace("The mouse hits a snowflake")
     }
}
...is not working.
 
New Member
Joined
Nov 24, 2001
Messages
692
Best answers
0
if vlok.hitTestPoint(parent.mouseX, parent.mouseY, true)

?
 
Member
✔️ HL Verified
🌟 Senior Member
Joined
Oct 16, 2006
Messages
379
Best answers
0
Location
the Netherlands
don't forget, you're only setting the event listener on the last vlok object (because you're adding the event listener outside the for loop)
As for intro, it can be done like this;

Code:
function intro(e:Event):void
{
     var thisVlok:DisplayObject = e.target as DisplayObject;
     if (thisVlok.hitTestPoint(parent.mouseX, parent.mouseY, true))
     {
        trace("The mouse hits a snowflake");
     }
}
 
New Member
✔️ HL Verified
💻 Oldtimer
Joined
Mar 29, 2003
Messages
4,765
Best answers
0
Location
The Netherlands
The If statement fixed the looping, thanks for that harSens. I don't know how I could've been so stupid to miss that part. :p I'm still having trouble to get it to work though. It just doesn't function no matter where I place it. Maybe I should try to think of something different for a hittest, since this is probably above my current skills.
 

Users who are viewing this thread

Top Bottom