Copy link to clipboard
Copied
I am a rookie flash developer, and I have a unique scenario I need to create. I need to put out a fire, only when the mouse is moving horizontally left and right overtop of the fire movieclip.
Right now I have some code that does the trick but if the mouse is going up and down the fire is supposed to grow. And my code is just sketchy.
function everything () {
controlMC.buttonMode = true;
Mouse.hide();
controlMC.startDrag(true);
var prevX:int = 0;
var prevY:int = 0;
var curX:int = 0;
var curY:int = 0;
import flash.net.*;
function Start()
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, CheckDirection);
}
Start();
function CheckDirection(e:MouseEvent)
{
setChildIndex(controlMC,numChildren - 1);
getDirection();
e.updateAfterEvent();
}
function getDirection(){
prevX = curX;
curX = stage.mouseX;
fireMC.scaleX = fireMC.scaleY;
prevY = curY;
curY = stage.mouseY;
success();
if (prevX > curX || prevX < curX && fireMC.hitTestPoint(mouseX,mouseY,true)) {
fireMC.height = fireMC.height - 5;
}
else if (prevY > curY || prevY < curY && fireMC.hitTestPoint(mouseX,mouseY,true)) {
fireMC.height = fireMC.height + 5;
}
function success()
{
if (fireMC.height <= 200) {
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdown);
Mouse.show();
controlMC.stopDrag();
TweenLite.to(fireMC, 1, {alpha: 0});
TweenLite.to(yesMC, .5, {alpha: 1});
setChildIndex(yesMC,numChildren - 1);
controlMC.visible = false;
yesMC.continueMC.addEventListener(MouseEvent.MOUSE_DOWN, fin);
yesMC.continueMC.buttonMode = true;
function fin () {
yesMC.continueMC.buttonMode = false;
yesMC.continueMC.removeEventListener(MouseEvent.MOUSE_DOWN, fin);
TweenLite.to(yesMC, .5, {alpha: 0});
setChildIndex(yesMC, 0);
gotoAndStop(1);
}
}
}
}
}
Feel free to tell me I'm a terrible as3 coder. I don't even understand classes
Copy link to clipboard
Copied
wow that works ? lol just kidding but ya its a lil hard to look at .
I think the issue is inside your if statement the condition asks if coridnate is less then or greater then current or previous position. Since you are checking down to the exact pixel it is easy to move the mouse where only the first condition get fired. What you want is a threshhold of maybe 5 pixels to eliminate any unwanted movement.
so
if (prevX > curX || prevX < curX && fireMC.hitTestPoint(mouseX,mouseY,true)) {
fireMC.height = fireMC.height - 5;
}
else if (prevY > curY || prevY < curY && fireMC.hitTestPoint(mouseX,mouseY,true)) {
fireMC.height = fireMC.height + 5;
}
should be
if(fireMC.hitTestPoint(curX,curY,true) {
if (prevX > (curX-5) || prevX < (curX+5)) {
fireMC.height = fireMC.height - 5;
}
else if (prevY > (curY-5) || prevY < (curY+5)) {
fireMC.height = fireMC.height + 5;
}
}
Copy link to clipboard
Copied
haha yeah it works. thanks for your code its cleaner and more accurate. but the else if is still doing the same thing. it shrinks the fire as well. it seems like even when trying to move vertically, there is still slight horizontal movement that throws everything off
Copy link to clipboard
Copied
oh i think i got the - and + backwards for both if statements , if you change those does it work
Copy link to clipboard
Copied
it does! You made my day. I always just read these kind of posts and get code from them this is my first time asking a question. And it went very well. Thank you!
Copy link to clipboard
Copied
glad it worked, good luck!
Copy link to clipboard
Copied
Since I have the attention of a knowledgable as3 developer....
What do you know about changing as2 to as3?
import flash.display.BitmapData;
var currentBitmap:String = "smoke_clear.png";
function doTrail(container:MovieClip, targetX:Number, targetY:Number, type:String):Void
{
var myBmp:BitmapData = BitmapData.loadBitmap(currentBitmap);
var _particle = container.createEmptyMovieClip("main_holder"+container.getNextHighestDepth(), container.getNextHighestDepth());
var internal_holder:MovieClip = _particle.createEmptyMovieClip("internal_holder", _particle.getNextHighestDepth());
internal_holder._x = -myBmp.width/2;
internal_holder._y = -myBmp.height/2;
internal_holder.attachBitmap(myBmp, internal_holder.getNextHighestDepth());
_particle._x = targetX + random(4)-8;
_particle._y = targetY + random(4)-8;
_particle._rotation = random(360);
var randomScale = random(50)+50;
_particle._xscale = randomScale;
_particle._yscale = randomScale;
_particle.speed = random(5)+3;
_particle.onEnterFrame = function ()
{
this._xscale += this.speed;
this._yscale += this.speed;
this._alpha -= this.speed;
if(this._alpha <= 0)
{
delete this.onEnterFrame;
removeMovieClip(this);
}
}
}
_root.onEnterFrame = function():Void
{
doTrail(_root, _xmouse, _ymouse, currentBitmap);
}
And do you know any easy ways to restart an swf on click?
Copy link to clipboard
Copied
AS3 below you need to set the smoke image properties in the library to export for actionscript and in first frame and name the class name smoke_clear. then in bold set the size of the bitmap where it says 25,25.
restarting a swf on click depends on AS2 or AS3 and if it is timelined animation or scripted animation or a mix of both.
/*
code below
*/
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.DisplayObject;
var container:MovieClip = new MovieClip();
addChild(container);
function doTrail(mcContainer:MovieClip, targetX:Number, targetY:Number):void
{
var myBmpDta:BitmapData = new smoke_clear(25,25);
var myBmp:Bitmap = new Bitmap(myBmpDta);
var _particle:MovieClip = new MovieClip();
var internal_holder:MovieClip = new MovieClip();
internal_holder.x = - myBmp.width / 2;
internal_holder.y = - myBmp.height / 2;
internal_holder.addChildAt(myBmp,internal_holder.numChildren);
_particle.addChildAt(internal_holder,_particle.numChildren);
_particle.x = targetX + Math.floor(Math.random() * 5) - 8;
_particle.y = targetY + Math.floor(Math.random() * 5) - 8;
_particle.rotation = Math.floor(Math.random() * 360);
mcContainer.addChildAt(_particle, mcContainer.numChildren);
var randomScale = (Math.floor(Math.random() * 51) + 50) / 100;
_particle.scaleX = randomScale;
_particle.scaleY = randomScale;
_particle.speed = Math.floor(Math.random() * 6) + 3;
_particle.addEventListener(Event.ENTER_FRAME, ptEnterFrame);
}
function myEnterFrame(evt:Event):void
{
doTrail(container, mouseX, mouseY);
}
function ptEnterFrame(evt:Event):void
{
evt.target.scaleX += evt.target.speed/100;
evt.target.scaleY += evt.target.speed/100;
evt.target.alpha -= evt.target.speed/100;
if (evt.target.alpha <= 0)
{
evt.target.removeEventListener(Event.ENTER_FRAME, ptEnterFrame);
container.removeChild(DisplayObject(evt.target));
}
}
this.addEventListener(Event.ENTER_FRAME, myEnterFrame);
Copy link to clipboard
Copied
You are amazing! Holy I love you. Thank you so much! The file I want to restart is as3 and there is no timeline animation, just 4 frames with code on each. All animation is tweenmax
Copy link to clipboard
Copied
your welcome
im not familiar with that tween class but ill try to help. sometimes those classes have a function you can call that will reset the tweens for reuse without recreating them but if you have different objects on different frames you might just want to jump back to frame one. Is the click action a specific button, movieclip or anywhere?
Copy link to clipboard
Copied
When I do go back to the first frame some of the movieclips don't go away and the functionality of the swf is very wonky... it's hard to explain the issues, maybe I could show you the file on monday?
Copy link to clipboard
Copied
sure ill be unavailabe till then anyways lol
Copy link to clipboard
Copied
Hey!
I really appreciate that you're taking such an interest in my as3 issues.
My game needs to be replayed over and over every time the user fails. I only have four frames and going to frame one does not solve the issue.
For example, this is the 4th and most compicated frame I have,
stop();
fireMC.x = 400;
fireMC.y = 450;
fireMC.width = 600;
fireMC.height = 670;
everything();
var snd2:Sound = new spraySnd();
var channel2:SoundChannel = snd2.play(0, 9999);
/*FireMovieClip.name = "FireMovieClipMC";
FireMovieClip1.name = "FireMovieClip1MC";
FireMovieClip2.name = "FireMovieClip2MC";
FireMovieClip3.name = "FireMovieClip3MC";*/
/*FireMovieClip.x = 280;
FireMovieClip.y = 380;
FireMovieClip1.x = 480;
FireMovieClip1.y = 400;
FireMovieClip2.x = 520;
FireMovieClip2.y = 430;
FireMovieClip3.x = 400;
FireMovieClip3.y = 465;
*/
function everything () {
controlMC.buttonMode = true;
Mouse.hide();
controlMC.startDrag(true);
var prevX:int = 0;
var prevY:int = 0;
var curX:int = 0;
var curY:int = 0;
import flash.net.*;
function Start()
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, CheckDirection);
}
Start();
function CheckDirection(e:MouseEvent)
{
setChildIndex(controlMC,numChildren - 1);
getDirection();
e.updateAfterEvent();
}
function getDirection(){
prevX = curX;
curX = stage.mouseX;
fireMC.scaleX = fireMC.scaleY;
/*FireMovieClip.scaleX = FireMovieClip.scaleY;
FireMovieClip1.scaleX = FireMovieClip1.scaleY;
FireMovieClip2.scaleX = FireMovieClip2.scaleY;
FireMovieClip3.scaleX = FireMovieClip3.scaleY;*/
prevY = curY;
curY = stage.mouseY;
success();
if(bbqMC.hitTestPoint(curX,curY,true)) {
if (prevX > (curX+5) || prevX < (curX-5)) {
fireMC.height = fireMC.height - 5;
/* FireMovieClip.height = FireMovieClip.height - 2;
FireMovieClip1.height = FireMovieClip1.height - 1;
FireMovieClip2.height = FireMovieClip2.height - 3;
FireMovieClip3.height = FireMovieClip3.height - 4;*/
} else if (prevY > (curY+5) || prevY < (curY-5)) {
fireMC.height = fireMC.height + 5;
/*FireMovieClip.height = FireMovieClip.height + 2;
FireMovieClip1.height = FireMovieClip1.height + 1;
FireMovieClip2.height = FireMovieClip2.height + 3;
FireMovieClip3.height = FireMovieClip3.height + 4;*/
}
}
function success()
{
if (fireMC.height <= 200) {
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdown);
Mouse.show();
controlMC.stopDrag();
TweenLite.to(fireMC, 1, {alpha: 0});
/*TweenLite.to(FireMovieClip, 1, {alpha: 0});
TweenLite.to(FireMovieClip1, 1, {alpha: 0});
TweenLite.to(FireMovieClip2, 1, {alpha: 0});
TweenLite.to(FireMovieClip3, 1, {alpha: 0});*/
setChildIndex(yesMC,numChildren - 1);
TweenLite.to(yesMC, .5, {alpha: 1});
channel1.stop();
channel2.stop();
controlMC.visible = false;
yesMC.continueMC.addEventListener(MouseEvent.MOUSE_DOWN, fin);
yesMC.continueMC.buttonMode = true;
function fin () {
yesMC.continueMC.buttonMode = false;
yesMC.continueMC.removeEventListener(MouseEvent.MOUSE_DOWN, fin);
TweenLite.to(yesMC, .5, {alpha: 0});
setChildIndex(yesMC, 0);
gotoAndStop(1);
}
}
}
}
}
///////
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.DisplayObject;
var container:MovieClip = new MovieClip();
addChild(container);
function doTrail(mcContainer:MovieClip, targetX:Number, targetY:Number):void
{
var myBmpDta:BitmapData = new smoke_clear(50,50);
var myBmp:Bitmap = new Bitmap(myBmpDta);
var _particle:MovieClip = new MovieClip();
var internal_holder:MovieClip = new MovieClip();
internal_holder.x = - myBmp.width / 2;
internal_holder.y = - myBmp.height / 2;
internal_holder.addChildAt(myBmp,internal_holder.numChildren);
_particle.addChildAt(internal_holder,_particle.numChildren);
_particle.x = targetX + Math.floor(Math.random() * 5) - 8;
_particle.y = targetY + Math.floor(Math.random() * 5) - 8;
_particle.rotation = Math.floor(Math.random() * 360);
mcContainer.addChildAt(_particle, mcContainer.numChildren);
var randomScale = (Math.floor(Math.random() * 51) + 50) / 100;
_particle.scaleX = randomScale;
_particle.scaleY = randomScale;
_particle.speed = Math.floor(Math.random() * 5) + 1;
_particle.addEventListener(Event.ENTER_FRAME, ptEnterFrame);
}
function myEnterFrame(evt:Event):void
{
doTrail(container, mouseX, mouseY);
}
function ptEnterFrame(evt:Event):void
{
evt.target.scaleX += evt.target.speed/100;
evt.target.scaleY += evt.target.speed/100;
evt.target.alpha -= evt.target.speed/100;
if (evt.target.alpha <= 0)
{
evt.target.removeEventListener(Event.ENTER_FRAME, ptEnterFrame);
container.removeChild(DisplayObject(evt.target));
}
}
this.addEventListener(Event.ENTER_FRAME, myEnterFrame);
Upon returning to frame one the yesMC which has the replay button in it is still present in the first frame even though it is not even placed in the first frame on the timeline.
I thought replaying the game would be the eay part
Find more inspiration, events, and resources on the new Adobe Community
Explore Now