A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: [RESOLVED] Need help condensing this

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Location
    Manchester, UK
    Posts
    25

    resolved [RESOLVED] Need help condensing this

    Hi guys,

    I need some help streamlining or condensing the code in the file attached. I know it can be done, but try as I might, as soon as I change it it starts screwing up.

    I need to be able to keep the code doing what it is doing at the moment (clicking on a dot, and then drawing a line from the start point to that dot), but I need to be able to do this for far more than just 3 dots, and I know that if I was to use my method of making this work, my code will end up being massive.

    So far, I've tried using variables in the drawLine function, but everytime I add one, loses the co-ordinates it needs to draw to.

    Also, once I'm done, I'd like to be able to make whichever dot is clicked, the new starting position for the next click, but that I'll tackle in a new thread, I think.

    Any help will be greatly appreciated.
    Attached Files Attached Files

  2. #2
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    I, for some reason, can't open the file in my flash cs4, so If possible, paste your code here.

    I don't know what you ae trying to do exactly, but if I understand correctly, you can simply push the coordinates of the dot being clicked in an array, and then in an enterFrame loop or something similar, check if the array is empty. If it is not empty, simply pop the coordinates from it, and draw lines to these coordinates.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Location
    Manchester, UK
    Posts
    25
    thanks for the quick reply and thanks for letting me know about the file - I'm using CS5, and just found out that I'd need to save as a CS4 file... anyway, here it is.

    All I need is a simplified or shorter way to write the code. One that will allow me to add more dots as time goes by without having to add a paragraph of code each time I add a dot. The last part about resetting the start point to the current one isn't that important at the moment.
    Attached Files Attached Files
    Last edited by mwwoggy; 08-29-2010 at 06:22 AM. Reason: added more info

  4. #4
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    well my as2 is really rusty, so i can't help you write actual code, but I can help you with logic.

    1) You don't need multiple drawline functions, write just one with a parameter
    Code:
    dot_1.onPress = function (){
    	dot = 1;
    	ep = "destination "+(dot);
    	drawLine1(dot_1); //passing dot_1 as parameter
    }
    
    function drawLine(dest){
    	_root.createEmptyMovieClip("line", 0);
    	line.lineStyle(3, 0, 100);
    	line.moveTo(start._x, start._y);
    	line.lineTo(dest._x, dest._y);
    	trace (dot);
    	trace (ep);
    }
    2) You can make dot_1, dot_2 e.t.c, instances of the same class, thereby having to write the code only once. (this is one of the reasons to use as3 over as2)
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  5. #5
    Junior Member
    Join Date
    Jun 2010
    Location
    Manchester, UK
    Posts
    25

    Thumbs up

    okay - seeing as I'm not that far into it, I can swap to AS3, how would you reccommend that I do it there? Please bear in mind that I'm a newbie with AS2, and even worse than that with AS3

    your idea workd perfectly - and it's so simple when you think about it thanks man

    If you don't mind, I'd still like to see how it's done in AS3, just so I can compare it and decide which way is the way foward

  6. #6
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    As3 is not as hard as people think it is, actually it is a lot easier once you stop listening to rumors and try it out for yourself.

    Anyway, I don't know exactly what you are trying to do, so I can't give you exact code to your situation. First, I will try to rewrite your code in as3, so that you can see the difference
    Code:
    //Assign a click event listener to the dots, they call the same function when clicked
    dot_1.addEventListener(MouseEvent.CLICK,drawLineToThis);
    dot_2.addEventListener(MouseEvent.CLICK,drawLineToThis);
    dot_3.addEventListener(MouseEvent.CLICK,drawLineToThis);
    
    //draw the line to the object clicked
    function drawLineToThis(e:MouseEvent)
    {
    	this.graphics.clear();
    	this.graphics.lineStyle(2,0x000000);
    	this.graphics.moveTo(start.x,start.y);
    	this.graphics.lineTo(e.target.x,e.target.y); //e.target gives the instance which fired this event
             //You can use this for your traces too. try trace(e.target.name);   
    }
    This is the basic thing without your traces..... you can put this on the timeline and then publish to as3 to get things working.


    But later you can make it more complex depending on your situation, for example, Write a "dot" class, and inside it dispatch a click event.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  7. #7
    Junior Member
    Join Date
    Jun 2010
    Location
    Manchester, UK
    Posts
    25
    Thanks for all the help, it's really appreciated. I think I'll try out AS3 and see how I get on.

  8. #8
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    glad I could help.

    Though it's a big misconception that with as3, you can't code on the timeline like as2, but if you are going to use as3, I would strongly recommend not coding on the timeline. Break your game into classes and code, that is the general way for programmers, and once you start, it will seem a lot easier, faster, and better.

    Download flashdevelop ( http://flashdevelop.org ) , a freeware for editing code, and you will have a much better experience with writing code (...nowadays, I wonder how I ever managed without it when I started in flash). If you get stuck anywhere, feel free to ask me.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center