dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: sidescrolling game, help needed!

  1. #1
    Senior Member
    Join Date
    Mar 2005
    Location
    london/ny
    Posts
    126

    sidescrolling game, help needed!

    i've created all the art for my game and now i need a bit of help building the engine...!
    the game features a scrolling background and a character running on the screen, continuously. the user only controls when the character jumps. otherwise he just runs. i have that working, the bckground scrolls continuously and the character runs. ok. i want enemies to randomly appear from the right hand side of the screen and move across to the left and leave the screen. the character has to avoid some enemies and jump on others for points. that's basically it. i'm not sure how the random function works and how i can apply it to three different types of enemy for this game.
    any help would be appreciiated. thanks!
    emma.

  2. #2
    Senior Member lowestformofwit's Avatar
    Join Date
    Jun 2004
    Location
    Scotland
    Posts
    160
    easy way to make moving baddies.

    1. take an enemy and turn it into a movieclip (F8), if not already done so.

    2. place an instance of the enemy just off stage, roughly where you want it to start running from.

    3. click on the enemy first and hit (F9) to bring up the actions panel.

    4. you want to place some code in the enemy to make him move. type in the following, ive put comments at each stage to make it clearer.

    code:

    // use an enterframe event so that it runs this code every frame

    onClipEvent(enterFrame){

    // first check if enemy is still on the stage

    if (this._x > 0 ){

    // if still on the stage move enemy 5 pixels to the left
    this._x -= 5;

    }
    // if not on the stage...
    else {
    // generate random number, put in variable called 'offset'. This will generate random numbers from 1 to 50.
    var offset = Math.random() *49+1;

    // reset horizontal position of enemy adding the offset to allow the generation of new baddies to be more random.
    this._x = 550+offset;
    }
    }



    To apply to the other baddies just follow the same procedure and place at different starting points on the stage.

    That'll get you started. It's not smart or clever but it's easy and a good introduction into actionscripting.

  3. #3
    Senior Member
    Join Date
    Mar 2005
    Location
    london/ny
    Posts
    126
    thank you very much for that! i've tried it out but as soon as the enemy disappears at the left hand side of the screen another one appears on the right, the random thing doesn't seem to be working...?? it's the same every time. is there a way as well that i can make the game choose randomly from a group of three different enemies so that it's random which one appears each time? i have each enemy as a movie clip...

  4. #4
    Senior Member mandissected's Avatar
    Join Date
    Jan 2005
    Location
    Berwick - England
    Posts
    338
    something like -

    randomenemy = Math.random()

    if (randomenemy >= 0 and randomenemy < 0.3) {
    get enemy1
    }

    if (randomenemy >= 0.3 and randomenemy < 0.6) {
    get enemy2
    }

    if (randomenemy >= 0.6 and randomenemy <= 1) {
    get enemy1
    }

  5. #5
    Senior Member
    Join Date
    Mar 2005
    Location
    london/ny
    Posts
    126
    oooh. so where would i put that code and where would i define the enemies as 1,2 and 3? (i take it the third bit of code is meant to be 'enemy3)
    and when i say get, where does it put it? sorry for being dense!

  6. #6
    Senior Member mandissected's Avatar
    Join Date
    Jan 2005
    Location
    Berwick - England
    Posts
    338
    yeah the 3rd was meant to be enemy 3 didn't spot that so you can't be as dense as me.

    you can just attach/duplicate mc of each enemy where it says get enemyx

  7. #7
    Senior Member lowestformofwit's Avatar
    Join Date
    Jun 2004
    Location
    Scotland
    Posts
    160
    I was trying to avoid complicating things by not dynamically creating instances of movie clips using attach or duplicate. I was just trying to make things really as basic as possible so you can learn the interactions of movie clips with code.

    In answer to your question, you can increase the randomness (if there is such a word) by increasing the value '49' on the line shown below:

    code:

    var offset = Math.random() *49+1;



    The above generates a number between 1 and 50.

    What you are essentially doing is moving your bad guy from the left of the stage back across to the right, ie. when its 'X' position reaches 0, change it back to 550. To make it random we are adding a value to the 550 position so it can be anywhere from 551 to 600.

    Its not a lot. Especially if you are running the game at a fast frame rate. You might want to generate a bigger number. Play with the figure 49 above until you get something you are happy with.

    Im also assuming your stage is the default size 550x400. If not you would have to change 550 to match your stage size.

    Hope that makes sense.

  8. #8
    Senior Member
    Join Date
    Mar 2005
    Location
    london/ny
    Posts
    126
    ok, let me show you what i've gone with now...
    Code:
    //create moving function
    function moving(){
    	this._x-=this.speed;
    	if(this._x < -100){
    		this.unloadMovie();
    	}
    }
    //create random enemies
    num = Math.round(Math.random()*10);
    switch (num) {
    case 0 || 1 || 2 :
    	//place barrel with attachMovie
    	_root.attachMovie("waste", waste1, i);
    	_root[waste1]._x = 700;
    	_root[waste1]._y = 405;
    	_root[waste1].speed = 5;
    	_root[waste1].onEnterFrame = moving;
    	break;
    case 3 || 4 || 5 || 6 :
    	//place chicken
    	_root.attachMovie("chicken", chicken1, i);
    	_root[chicken1]._x = 700;
    	_root[chicken1]._y = 335.9;
    	_root[chicken1].speed = 7;
    	_root[chicken1].onEnterFrame = moving;
    	break;
    case 7 || 8 || 9 :
    	//place egg
    	_root.attachMovie("egg", egg1, i);
    	_root[egg1]._x = 700;
    	_root[egg1]._y = 393.2;
    	_root[egg1].speed = 5;
    	_root[egg1].onEnterFrame = moving;
    	break;
    default :
    	//place nothing
    	break;
    }
    but it doesn't work and i don't know why

  9. #9
    Senior Member
    Join Date
    Mar 2005
    Location
    london/ny
    Posts
    126
    oh and i have the simpler method working in another save file of it... it works ok and i might go for that if this method doesn't work. it's nice and random at least - have upped the numbers to the high hundereds and that's better.

  10. #10
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    _root[waste1]._x = 700;

    This is setting _root.undefined._x to 700, not _root.waste1._x to 700. You haven't set what the variable waste1's value is.
    http://www.birchlabs.co.uk/
    You know you want to.

  11. #11
    Senior Member
    Join Date
    Mar 2005
    Location
    london/ny
    Posts
    126
    oh, i thought that was what i was going with

    _root.attachMovie("waste", waste1, i);

    ...
    so if i write

    _root.waste1._x = 700

    and forget about the brackets will that fix it??

  12. #12
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,367
    Yes, that will fix it...

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