Results 1 to 3 of 3

Thread: Type of Obj incompatible with expected type parameter for obj

  1. #1
    Join Date
    Dec 2011
    Posts
    34
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Type of Obj incompatible with expected type parameter for obj

    I don't get it, if I declare things manually it works fine, if I use a constructor to make the same object with same set of properties I can console.log that the correct properties and values are tied to that object. But when i go to use the new object with it's properties the same way as the manually declared ones, it throws the error in the title.

    The working "manual" version: (This part works fine)
    Code:
    var rat1={
        image:ratImage,
        srcX:0,
        srcY:0,
        sizeX:50,
        sizeY:50,
        x: 0,
        y: 0,
        destW:50,
        destH:50,
        active:false
        };
    var rat2={
        image:ratImage,
        srcX:0,
        srcY:0,
        sizeX:50,
        sizeY:50,
        x: 0,
        y: 0,
        destW:50,
        destH:50,
        active:false
        };
    
    //Then in my render() function
    ctx.drawImage(rat1.image, rat1.srcX,rat1.srcY, 50,50, bgimage.x+ rat1.x, bgimage.y+rat1.y,50,50);
    ctx.drawImage(rat2.image, rat2.srcX,rat2.srcY, 50,50, bgimage.x+ rat2.x, bgimage.y+rat2.y,50,50);
    That works fine, it draws two rats from the same image, ratImage, which is declared elsewhere and referenced by that variable. I move the rat by using it's x, y properties, so (rat1.x, rat1.y) or shuffle it's frame in a sprite sheet with rat1.srcX+=single_frame_distance

    So I tried to make my first constructor, which seems really awesome.

    Code:
    function Rat(image,srcX,srcY,sizeX,sizeY,x,y,destW,destH,active){   
        this.image=image;
        this.srcX=srcX;
        this.srcY=srcY;
        this.sizeX=sizeX;
        this.sizeY=sizeY;
        this.x= x;
        this.y= y;
        this.destW=destW;
        this.destH=destH;
        this.active=active;
        console.log('RAT INSTANTIATED: ' + this.image); //outputs ratImage
        console.log('RAT:active: ' + this.active); //outputs false
        }
    
    var rat3= new Rat('ratImage', 0,0, 50,50, 90,50, 50,50, 'false');
    var rat4= new Rat('ratImage', 0,0, 50,50, 50,90, 50,50, 'false');
    
    console.log('rat3: '+rat3.image); //outputs ratImage
    console.log('rat3: '+rat3.active); //outputs false
    It makes new rat objects, it gives them all the properties, I can console.log the properties on the newly created objects to verify it has the right property and value.

    But the second I copy the drawImage section that worked for rat1 and rat2, and substitute the newly created rat3 and rat4 objects for rat1 and rat2, it throws the error in the title.

    This part doesn't work
    Code:
     ctx.drawImage(rat3.image, rat3.srcX, rat3.srcY, rat3.sizeX,rat3.sizeY, bgimage.x+ rat3.x, bgimage.y+rat3.y,rat3.destW,rat3.destH);
    //or with constant height/widths, tested both ways
    ctx.drawImage(rat4.image, rat4.srcX, rat4.srcY, 50,50, bgimage.x+ rat4.x, bgimage.y+rat4.y,50,50);
    If I remove the '' quote marks around the image and "active" property it throws undefined and breaks even more, other than that I can't figure out what "type" it's expecting.......

    Is it because of the ratImage? It's a variable, the path to the image is ratImage.src=".../path/rat.gif"

    That's the only difference I can tell between the "manual" version compared to the "constructed" one.

    Any ideas????

    EDIT:: Looks like it is the image part of it. If I get rid of rat3.image and rat4.image and just place the variable of the image like:
    ctx.drawImage(ratImage, rat3.srcX, rat3.srcY, rat3.sizeX,rat3.sizeY, bgimage.x+ rat3.x, bgimage.y+rat3.y,rat3.destW,rat3.destH);

    ctx.drawImage(ratImage, rat4.srcX, rat4.srcY, rat4.sizeX,rat4.sizeY, bgimage.x+ rat4.x, bgimage.y+rat4.y,rat4.destW,rat4.destH);

    It works...I guess I can work with that, but out of curiosity is there a way to tie an image variable to the object as a property like I was trying to do in the constructor?
    Last edited by lmbarns; 01-11-2012 at 06:01 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,954
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    In this part of the code from your post:

    Code:
    var rat3= new Rat('ratImage', 0,0, 50,50, 90,50, 50,50, 'false');
    var rat4= new Rat('ratImage', 0,0, 50,50, 50,90, 50,50, 'false');
    The highlighted are all literal strings because they are quoted. What should work (as long as everything else is OK) is:

    Code:
    var rat3= new Rat(ratImage, 0,0, 50,50, 90,50, 50,50, false);
    var rat4= new Rat(ratImage, 0,0, 50,50, 50,90, 50,50, false);
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Dec 2011
    Posts
    34
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the clarification. It ended up still not working, then I realized I'm an idiot. Dropping the quotes made the "active" property a boolean, which is what I wanted, but still gave the rat3.image an "undefined" value.

    I then realized that I was declaring ratImage further down in the code, I moved the new rat variables below where ratImage was declared and it magically wasn't undefined!

    Thanks a lot, I was all over the place wondering what else and you helped me narrow it down. Works perfectly now

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •