dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Problem with shooting "lasers"

  1. #1
    Senior Member
    Join Date
    Feb 2005
    Posts
    177

    Problem with shooting "lasers"

    ok i have a game called star gun adventure in which you control a character with the up and down arrows and space to fire. Im haveing trouble with the "fire" part. I have code so that like in all shooting games, when you press the shoot button the bullet comes fromt he gun and goes out wards. In my 3dfa screen it works great and whenever i press spacebar the bullet appears by my gun and shoots out wards. But in my browser screen it doesnt work. Whenever i press spacebar, the bullet doesnt not appear at my gun and shoot out wards, but it shoots from the same place in which i created it.

    I think my code that makes the bullet share the same x and y data is wrong for in my browser the bullet doesnt appear at my gun and shoot out for it shoots from the same place.

    i have a file so you can see if my code is wrong.

    thanks
    Attached Files Attached Files
    Last edited by sonic04; 04-10-2005 at 09:50 AM.

  2. #2
    A Senior Newbie - How Odd ForumNewbie's Avatar
    Join Date
    Mar 2003
    Posts
    590

    When to use 'if' statements and when to use '=' and '=='

    Looks like you're misusing 'if' statements here, so here's a mini lesson:

    The operator '...=...' sets the first part of the equation to be the value of the second. The operator 'if (...==...)' tests if the first part equals the second part and then executes the next piece of code if this is true.

    In your script, do you mean to be setting a variable, or testing one?
    If you're setting a variable, you don't need the 'if'.
    If you're testing a variable, you need to put == instead of =. Also, you then need to put some code in to say what you want it to do if the test is true.

    The code you've written is ignoring the 'if' statement because you have only one '=' sign. Even if the 'if' wasn't being ignored, the code wouldn't do much, because you haven't said what you want it to do if the expression comes back as 'true'. This bad code is possibly getting interpreted differently in the browser to 3DFA, but is wrong in either case.

    Some examples:

    if (bob=mary)
    Will set bob equal to what mary was, because there's only one '=', but is bad code and might appear differently in the browser to 3DFA.

    bob=mary
    Is the right way to do this. This will set bob to the value of mary.

    if (bob==mary)
    {
    terry=june
    }
    Will check to see if bob equals mary, and if it does, sets terry equal to june.

    if (bob==mary)
    {
    terry=june
    }
    else
    {
    terry=fred
    }
    Will check to see if bob equals mary, and if it does, sets terry equal to june, and if it doesn't, will set terry equal to fred.

    I suspect you don't want to use 'if' here, and just need to change your script so that the bullet appears a little lower down and to the right.

    Try this instead:
    element("bullet").x=element("gun").x+40
    element("bullet").y=element("gun").y+15
    element("bullet").velocity.x=element("gun").veloci ty.x
    element("bullet").velocity.y=element("gun").veloci ty.y

    Alternatively, move the starting points of the elements to be in line to avoid having to offset the bullet with script.

    Morgan.
    Please note that my domain has changed to http://www.morganmultinational.com

  3. #3
    Senior Member
    Join Date
    Feb 2005
    Posts
    177
    Thanks again.

    I got it to work now.

    lol by the way bob, mary, terry, june, and fred have helped me a lot.

    thanks

    jaron

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