Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
javascript problem z obiektem
#1
Witam. Mam następujący problem: Utworzyłem konstruktor odpowiedzialny za ruch animowanego ptaka na stronie. tworzę obiekt 'sparrow' z tego konstruktora i wszystko pięknie gra:ptaszek fruwa sobie po stronie. Chcę utworzyć kolejny obiekt aby zachowywał sie tak jak pierwszy. I gdy tworzę 'sparrow1', pierwszy obiekt przestaje fruwać, a zaczyna drugi. i Tak gdy utworzę pięć obiektów z konstruktora, tylko ostatnio utworzony działa. a chciałbym żeby wszystkie fruwały. Pytanie brzmi, jak osiągnąć taki efekt??
oto kod:
Kod:
function spriteAnimation(elementId,pxjumpY,spriteNumber,movePx, moveSpeed){
    ths=this;
    this.element=document.getElementById(elementId);
    // -funkcja jump start
    this.jump=pxjumpY;
    this.spriteNumber=spriteNumber;
    this.spriteJump=0;
    this.jumpCounter=0;
    // -funkcja  jump end
    // -funkcja  getParentInfo start
    this.parentWidth;
    this.parentHeight;
    // -funkcja  getParentInfo end
    // -funkcja  getElementPosition start
    this.elementPositionX;
    this.elementPositionY;
    // -funkcja  getElementPosition end
    // -funkcja  getDirection start
    this.birdAtBottom=false;
    this.birdAtTop=false;
    this.birdAtLeft=true;
    this.birdAtRight=false;
    // -funkcja  getDirection end
    // -funkcja  getElementPosition i move start
    this.moveY=10;
    this.moveX=10;
    // -funkcja  getElementPosition i move end
    // -funkcja  randomValue start
    this.randValue;
    // -funkcja  randomValue end
    // -funkcja  getElementPosition i move<--
    this.isFlying=false;
    this.Height;
    this.Width;
    this.movePx=movePx;
    this.moveSpeed=moveSpeed;
    this.directionCounter=0;
    this.getElementInfo=function(){
    ths.Width=parseFloat(window.getComputedStyle(ths.element, null).getPropertyValue("width"));
    ths.Height=parseFloat(window.getComputedStyle(ths.element, null).getPropertyValue("height"));
    }();
    this.getParentInfo=function(){
    var parentElement=ths.element.parentNode;
    ths.parentWidth=parseFloat(window.getComputedStyle(parentElement, null).getPropertyValue("width"));
    ths.parentHeight=parseFloat(window.getComputedStyle(parentElement, null).getPropertyValue("height"));
    }();
    this.fly=function(){//podmiana sprite'a
        if(ths.birdAtLeft){
        ths.element.style.backgroundPosition="0px "+ths.spriteJump+"px";
        }else if(ths.birdAtRight){
        ths.element.style.backgroundPosition="75px "+ths.spriteJump+"px";    
        }
        ths.spriteJump+=54;
        ths.jumpCounter++;
        if(ths.jumpCounter==ths.spriteNumber){
            ths.spriteJump=0;
            ths.jumpCounter=0;
        }
        setTimeout(arguments.callee, 80);
    }
    this.getElementPosition=function(){
        ths.elementPositionX=parseFloat(window.getComputedStyle(ths.element, null).getPropertyValue("left"));
        ths.elementPositionY=parseFloat(window.getComputedStyle(ths.element, null).getPropertyValue("top"));
    };
    this.getDirection=function(){
    ths.getElementPosition();
        //gdy ptaszek sięgnął dołu
        if(ths.elementPositionY>ths.parentHeight-ths.Height-5){
            ths.birdAtBottom=true;
            ths.birdAtTop=false;
        }
        //gdy ptaszek sięgnął góry
        if(ths.elementPositionY<0){
            ths.birdAtTop=true;
            ths.birdAtBottom=false;
        }
        //gdy ptaszek sięgnął lewej
        if(ths.elementPositionX<0){
            ths.birdAtLeft=true;
            ths.birdAtRight=false;
        }
        //gdy ptaszek sięgnął prawej
        if(ths.elementPositionX>ths.parentWidth-ths.Width-5){
            ths.birdAtRight=true;
            ths.birdAtLeft=false;
        }
    };
    this.setDirection=function(){
        if(ths.birdAtTop){
            ths.moveY=ths.movePx;
        }
        if(ths.birdAtBottom){
            ths.moveY=-ths.movePx;
        }
        if(ths.birdAtLeft){
            ths.moveX=ths.movePx;
        }
        if(ths.birdAtRight){
            ths.moveX=-ths.movePx;
        };
    };
    this.randomValue=function(){
        if(Math.floor(Math.random()*10)>5){
            ths.randValue=ths.movePx;
        }else{
            ths.randValue=-ths.movePx;
        };
    }
        this.move=function(){
        ths.getElementPosition();
        ths.getDirection();
        ths.setDirection();
        ths.randomValue();
        ths.element.style.top=ths.elementPositionY+ths.moveY+"px";
        ths.element.style.left=ths.elementPositionX+ths.moveX+"px";
        setTimeout(arguments.callee, ths.moveSpeed);
    }
}


$(function(){
    var sparrow=new spriteAnimation('sparrow', 54,6,5,60);
    sparrow.fly();
    sparrow.move();
var sparrow1=new spriteAnimation('sparrow1', 54,6,5,100);
    sparrow1.fly();
    sparrow1.move();
})
Odpowiedz
#2
Odpowiedz
#3
Zdaje sobie sprawę, że trzeba tu sporo udoskonalićSmile. Popracuje jeszcze nad kodem, dzięki wielkie za wskazówki.
Odpowiedz


Podobne wątki…
Wątek: Autor Odpowiedzi: Wyświetleń: Ostatni post
  Czy ilość zmiennych w javascript, jako języku interpretowanym, ma znaczenie? Jakie gburhiphop 22 22,600 09-09-2016, 21:09
Ostatni post: Ribana
Toungue Problem z nie działającym javascript Radian 4 4,859 20-03-2015, 16:31
Ostatni post: Radian
  JavaScript podstawy bryndzyk 5 4,226 18-05-2013, 21:17
Ostatni post: ferrante
  JavaScript Gmaps Api Rysowanie Squezzer 0 2,080 16-05-2013, 18:18
Ostatni post: Squezzer
  itegracja lightboxa z "like button" z JavaScript SDK sakkada 4 4,801 04-01-2013, 01:39
Ostatni post: sakkada

Skocz do:


Użytkownicy przeglądający ten wątek:
Sponsorzy i przyjaciele
SeoHost.pl