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

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




make a sound play when i hit the food. but without looping

 
Reply to this topicStart new topic

make a sound play when i hit the food. but without looping, make my sound play when i get the food and it only plays once

cadeownz
post 15 Jul, 2008 - 04:37 PM
Post #1


New D.I.C Head

*
Joined: 11 Jul, 2008
Posts: 31

This is the complete code since i do not know where to attach it. Thanks in advance to anyone who can help me out with this.. biggrin.gif biggrin.gif biggrin.gif
CODE
var unit = 15;
var uwh = 20;
var canMove = false;
var dir = 2;
var score = 0;
aPieceList = new Array();
mouseListener = new Object();
mouseListener.onMouseDown = function()
{
        if (!canMove)
        {
                canMove = true;
                startGame();
        }
};
Mouse.addListener(mouseListener);
k = new Object();
k.onKeyDown = function()
{
        var k = Key.getCode();
        if (k == Key.UP && dir != 2 && canMove)
        {
                dir = 0;
                canMove = false;
        }
        else if (k == Key.LEFT && dir != 3 && canMove)
        {
                dir = 1;
                canMove = false;
        }
        else if (k == Key.DOWN && dir != 0 && canMove)
        {
                dir = 2;
                canMove = false;
        }
        else if (k == Key.RIGHT && dir != 1 && canMove)
        {
                dir = 3;
                canMove = false;
        }
};
Key.addListener(k);
function addPiece()
{
        var p = this.attachMovie("piece", "piece" + aPieceList.length, aPieceList.length);
        p._x = aPieceList[aPieceList.length - 1]._x;
        p._y = aPieceList[aPieceList.length - 1]._y;
        aPieceList.push(p);
}
function moveFood()
{
        var moveIt = true;
        while (moveIt)
        {
                food._x = Math.floor(Math.random() * uwh) * unit;
                food._y = Math.floor(Math.random() * uwh) * unit;
                moveIt = false;
                for (var i = 0; i < aPieceList.length; i++)
                {
                        if (aPieceList[i]._x == food._x && aPieceList[i]._y == food._y)
                        {
                                moveIt = true;
                        }
                }
        }
}
function gameOver()
{
        delete this.onEnterFrame;
        tScore.text = "GameOver"
        canMove = true;
        
        tScore2.text = ""+ score;
    
        canMove = false;
}
function startGame()
{
        for (var i = aPieceList.length - 1; i >= 0; i--)
        {
                aPieceList[i].removeMovieClip();
                aPieceList.pop();
        }
        score = 0;
        var p = this.attachMovie("piece", "piece" + aPieceList.length, aPieceList.length);
        aPieceList.push(p);
        p._x = 10 * unit;
        p._y = 10 * unit;
        var food = this.attachMovie("food", "food", -1);
        var c = 0;
        moveFood();
        var startingLength = 3;
        for (var i = 1; i < startingLength; i++)
        {
                addPiece();
        }
        this.onEnterFrame = function()
        {
                canMove = true;
                tScore.text = score;
                for (var i = aPieceList.length - 1; i > 0; i--)
                {
                        aPieceList[i]._x = aPieceList[i - 1]._x;
                        aPieceList[i]._y = aPieceList[i - 1]._y;
                }
                if (dir == 0)
                {
                        aPieceList[0]._y -= unit;
                }
                else if (dir == 1)
                {
                        aPieceList[0]._x -= unit;
                }
                else if (dir == 2)
                {
                        aPieceList[0]._y += unit;
                }
                else if (dir == 3)
                {
                        aPieceList[0]._x += unit;
                }
                if (aPieceList[0]._y / unit == 20)
                {
                        aPieceList[0]._y = 0;
                }
                else if (aPieceList[0]._y / unit == -1)
                {
                        aPieceList[0]._y = 19 * unit;
                }
                else if (aPieceList[0]._x / unit == -1)
                {
                        aPieceList[0]._x = 19 * unit;
                }
                else if (aPieceList[0]._x / unit == 20)
                {
                        aPieceList[0]._x = 0;
                }
                if (aPieceList[0]._x == food._x && aPieceList[0]._y == food._y)
                {
                        score += 10 * aPieceList.length / 2;
                        moveFood();
                        addPiece();
                }
                for (var i = 1; i < aPieceList.length; i++)
                {
                        if (aPieceList[0]._x == aPieceList[i]._x && aPieceList[0]._y == aPieceList[i]._y)
                        {
                                gameOver();
                        }
                }
        };
}
User is offlineProfile CardPM

Go to the top of the page

stayscrisp
post 15 Jul, 2008 - 05:14 PM
Post #2


D.I.C Regular

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



Thanked 6 times
My Contributions



Hey smile.gif

Ok i get what your saying, but one thing....

I can code but that doesn't really mean i can just look through someone's entire code and know how it all works, I can definitely help you with this but could you tell me where you are playing the sound, when you hit the cheese and just a small walkthrough of how you have attempted the problem. Also because your using flash i cant just copy and paste this code to test compile it.

do these things and the problem will be solved as I'm sure its relatively simple.

thanks
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:44AM

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