Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,602 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 954 people online right now. Registration is fast and FREE... Join Now!




FLASH: hitTest, creating more than one wall.

 
Reply to this topicStart new topic

FLASH: hitTest, creating more than one wall., FLASH: making more than one wall.

cadeownz
post 12 Jul, 2008 - 12:00 AM
Post #1


New D.I.C Head

*
Joined: 11 Jul, 2008
Posts: 31

I have made a simple RPG Game (Just Started) and i need to make more than ONE solid wall. Any help?
Code Below...


CODE
onClipEvent (enterFrame) {
    if (_root.go == true) {
        yspeed = yspeed+gravity;
        for (x=1; x<precision; x++) {
            spot_x = _x+radius*Math.sin(x*360/precision);
            spot_y = _y-radius*Math.cos(x*360/precision);
            if (_root.terrain.hitTest(spot_x, spot_y, true)) {
                yspeed = 0;
                }
        }
        _y = _y+yspeed;
        _x = _x+xspeed;
    }
}
User is offlineProfile CardPM

Go to the top of the page

BetaWar
post 12 Jul, 2008 - 01:22 AM
Post #2


#include <soul.h>

Group Icon
Joined: 7 Sep, 2006
Posts: 1,987



Thanked 78 times

Dream Kudos: 1175
My Contributions


There are many ways to make this work out, you may want to look at google for "console games" there is one that shows how to make an interestin console type game in flash.

One way that you may want to think about doing things, assuming that you don't have millions of different pieces of terrain on the screen at once (or checking at the same time) would be to add all of them to an array and just loop through them.

CODE

var terrain_array:Array = new Array();
    terrain_array.push(_root.terrain);
    //and so on...
onClipEvent(enterFrame){
    if(_root.go == true){
        yspeed = yspeed+gravity;
        for(var x:Number = 1; x<precision; x++){
            spot_x = _x+radius*Math.sin(x*360/precision);
            spot_y = _y-radius*Math.cos(x*360/precision);
            doHitCheck(spot_x, spot_y);
        }
    }
}
function doHitCheck(x:Number, y:Number){
    for(var i:Number = 0; i<terrain_array.length; i++){
        if(terrain_array[i].hitTest(x, y, true)){
            yspeed = 0;
            break;
        }
    }
    return;
}


That will check for everything you add into the terrain and see if every check hits it, if it does it will end the loop and continue the checks, other wise it will continue the loop until it finds one that works, or finishes going through the array of movieclips.

However, a better way, is to simply place all the ground that you want in a single movieclip and then to a hitTest against it.

Here is some sample code from a console game out there in one of those tutorials:

CODE
var jumpHeight = -20;

var options = _root.createEmptyMovieClip("opts",101);

var k = _root.createEmptyMovieClip("square", l00);
k.beginFill(0x989cc9);
k.moveTo(0, -5);
kpt = new Array({x:0, y:-5}, {x:0, y:5}, {x:5, y:5}, {x:5, y:-5});
for (var e = 1; e<kpt.length; e++) {
    k.lineTo(kpt[e].x, kpt[e].y);
}
sp = 0;
k._y = Stage.height/2;
k._x = Stage.width/2;
pts = new Array({x:23, y:-200}, {x:46, y:-200}, {x:46, y:333}, {x:233, y:333}, {x:290.9, y:273}, {x:480, y:332.4}, {x:620, y:266}, {x:620, y:230}, {x:641, y:230}, {x:641, y:176}, {x:681, y:176}, {x:681, y:250}, {x:740, y:335}, {x:900, y:335}, {x:900, y:-200}, {x:940, y:-200}, {x:940, y:381}, {x:23, y:382}, {x:23, y:-200});
_root.createEmptyMovieClip("plat", -1);
plat.lineStyle(10);
for (var i = 1; i<pts.length; i++) {
    plat.moveTo(pts[i-1].x, pts[i-1].y);
    plat.lineTo(pts[i].x, pts[i].y);
}
onEnterFrame = function () {
    if(plat.hitTest(square._x, square._y, true)){
        jumping=false;
    }
    else{
        square._y += 8;
    }
    while (plat.hitTest(square._x, square._y, true)) {
        square._y--;
    }
    if((!plat.hitTest(square._x+10, square._y-20, true) && sp<-1) || (!plat.hitTest(square._x-10, square._y-20, true) && sp>1)){
        plat._x += (sp *= .85);
    }
    else{
        (sp *= .85);
    }
    if(Key.isDown(Key.UP) && !jumping){
        jump=jumpHeight;
        jumping=true;
    }
    else{
        null;
    }

    if(jumping){
        if(jump<10){
            jump++;
        }
        else{
            jump=10;
        }
        square._y += jump;
    }
    else{
        null;
    }
    if(Key.isDown(Key.LEFT)){
        sp += .75;
    }
    else{
        null;
    }
    if(Key.isDown(Key.RIGHT)){
        sp--;
    }
    else{
        null;
    }

};


They just make a single movieclip and do all their checks agains it for grouund and walls.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

stayscrisp
post 12 Jul, 2008 - 07:52 AM
Post #3


D.I.C Regular

***
Joined: 14 Feb, 2008
Posts: 258



Thanked 6 times
My Contributions


question answered smile.gif

god i love flash , makes collision detection so easy!

This post has been edited by stayscrisp: 12 Jul, 2008 - 12:09 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:08AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month